0

I am trying to create and print a deck of cards and the following error is displayed

    Traceback (most recent call last):
  File "/Users/file/file/file/main.py", line 11, in <module>
    print(deck)
NameError: name 'deck' is not defined

I have tried replacing the values for numbers in range (1, 14) and it still says deck is not defined. I have also tried moving the deck = [] to before the def shuffle() and it then prints the list as []. Any idea what I can do to create and print the deck? I will be shuffling the order of the deck once created. Below is my code:

def shuffle():
    suits = ["Cups", "Pentacles", "Wands", "Swords"]
    values = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Knight", "King", "Queen"]

    global deck
    deck = []
    for suit in suits:
        for value in values:
            deck.append(f'{value}_of_{suit}')

print(deck)

I am fairly new to Python. I have tried different ways to create a deck but it doesn't seem to work well with my overall project (in tkinter). I have seen the above method of creating a deck of cards done by others in the same way and it seems to work for them so I can't see where I am going wrong.

3
  • 2
    Please check your indentation. Python requires correct indentation. As I read the code here, this won't work at all. Commented Oct 13, 2022 at 10:10
  • 1
    Maybe you forget to call shuffle() before printing deck. Commented Oct 13, 2022 at 10:14
  • 2
    don't use globals. call the function with deck = shuffle() and inside the function shuffle put return deck as the last instruction. and change the function name, since you are creating a deck, but not shuffling it. The deck will always have the same order Commented Oct 13, 2022 at 10:14

4 Answers 4

1

It returns the list value of the function and you can define a variable outside the function and assign the value of the function to this variable and use it everywhere. Or you define a class, you define a deck variable in this class. and with your function you assign a value to the variable deck

first solution:

def shuffle():
    suits = ["Cups", "Pentacles", "Wands", "Swords"]
    values = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Knight", "King", "Queen"]
    deck = []
    for suit in suits:
        for value in values:
            deck.append(f'{value}_of_{suit}')
    return deck

deck = shuffle()

second soluiton:

class A:
    
    def __init__(self):
        self.deck = []

    def shuffle(self):
        suits = ["Cups", "Pentacles", "Wands", "Swords"]
        values = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Knight", "King", "Queen"]

        for suit in suits:
            for value in values:
                self.deck.append(f'{value}_of_{suit}')
    
# class instance
instance = A()
# run function and create deck
instance.shuffle()

# print deck
print(instance.deck)
Sign up to request clarification or add additional context in comments.

3 Comments

The first solution doesn't declare deck, nor return it from shuffle
shouldn't it be print(instance.shuffle(deck))?
Thank you for your warning. I rearranged. In the first solution I wrote, I copied the code incorrectly.
1

There is a problem with indent on the code at the line global deck, once fixed the code worked perfectly.

def shuffle():
    suits = ["Cups", "Pentacles", "Wands", "Swords"]
    values = ["Two","Ace", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Knight", "King", "Queen"]

global deck
deck1 = []
for s in suits:
    for v in values:
        deck1.append(f'{v}_of_{s}')

Comments

1

I just tried your code and it works. The problem maybe can be caused by the bad identation required by python.

def shuffle():
    suits = ["Cups", "Pentacles", "Wands", "Swords"]
    values = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Knight", "King", "Queen"]

    deck = []
    for suit in suits:
        for value in values:
            deck.append(f'{value}_of_{suit}')
    return deck

deck = shuffle()

Output:

['Ace_of_Cups',
 'Two_of_Cups',
 'Three_of_Cups',
 'Four_of_Cups',
 'Five_of_Cups',
 'Six_of_Cups',
 'Seven_of_Cups',
 'Eight_of_Cups',
 'Nine_of_Cups',
 'Ten_of_Cups',
 'Knight_of_Cups',
 'King_of_Cups',
 'Queen_of_Cups',
 'Ace_of_Pentacles',
 'Two_of_Pentacles',
 'Three_of_Pentacles',
 'Four_of_Pentacles',
 'Five_of_Pentacles',
 'Six_of_Pentacles',
 'Seven_of_Pentacles',
 'Eight_of_Pentacles',
 'Nine_of_Pentacles',
 'Ten_of_Pentacles',
 'Knight_of_Pentacles',
 'King_of_Pentacles',
 'Queen_of_Pentacles',
 'Ace_of_Wands',
 'Two_of_Wands',
 'Three_of_Wands',
 'Four_of_Wands',
 'Five_of_Wands',
 'Six_of_Wands',
 'Seven_of_Wands',
 'Eight_of_Wands',
 'Nine_of_Wands',
 'Ten_of_Wands',
 'Knight_of_Wands',
 'King_of_Wands',
 'Queen_of_Wands',
 'Ace_of_Swords',
 'Two_of_Swords',
 'Three_of_Swords',
 'Four_of_Swords',
 'Five_of_Swords',
 'Six_of_Swords',
 'Seven_of_Swords',
 'Eight_of_Swords',
 'Nine_of_Swords',
 'Ten_of_Swords',
 'Knight_of_Swords',
 'King_of_Swords',
 'Queen_of_Swords']

1 Comment

maybe adding return value as said by @Sembei Norimaki
0

try out this one :

def shuffle():
    suits = ["Cups", "Pentacles", "Wands", "Swords"]
    values = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Knight", "King", "Queen"]

    global deck
    deck = []
    for suit in suits:
        for value in values:
            deck.append(f'{value}_of_{suit}')
    return deck

e=shuffle()
print(e)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.