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.
shuffle()before printingdeck.deck = shuffle()and inside the function shuffle putreturn deckas 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