Home > database >  How do I define an empty python list and append values, without the NameError?
How do I define an empty python list and append values, without the NameError?

Time:10-13

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.

CodePudding user response:

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"]

    global deck
    deck = []
    for suit in suits:
        for value in values:
            deck.append(f'{value}_of_{suit}')
            
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']

CodePudding user response:

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)

CodePudding user response:

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)

CodePudding user response:

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}')
  • Related