import random
def create_deck():
suits = ["♥","♣","♦","♠"]
values = ["1","2","3","4","5","6","7","8","9","10","J","Q","K"]
deck = []
card = ""
for suit in suits:
for value in values:
card = value + suit
deck.append(card)
random.shuffle(deck)
random.shuffle(deck)
cards = deck[:21]
print(cards)
return cards
#create the deck and keep 21 cards
def create_piles(deck):
pile1 = []
pile2 = []
pile3 = []
for i in range(7):
pile1.append(cards.pop())
pile2.append(cards.pop())
pile3.append(cards.pop())
print("\n" *3)
print(pile1)
print(pile2)
print(pile3)
return pile1,pile2,pile3
#create the three piles
def user_choice():
while True:
print("What pile is your card in?")
choice = input("----> ")
print(cards)
if choice in ["1","2","3"]:
return choice
#select the pile
def reassemble(choice,pile1,pile2,pile3):
if choice == "1":
cards = pile2 + pile1 + pile3
elif choice == "2":
cards = pile1 + pile2 + pile3
else:
cards = pile2 + pile3 + pile1
#reassemble the piles
def win(create_piles):
print("Your card was")
#the card that the user picked
def trick_compile(cards):
for i in range(3):
pile1,pile2,pile3 = create_piles(cards)
choice = user_choice()
cards = reassemble(choice,pile1,pile2,pile3)
win(create_piles)
quit()
#framework that runs the trick
cards = create_deck()
trick_compile(cards)
I am a GCSE Computer Science student. This code is based off last years coursework, which asks for a card trick.
When I run my script, the following error is displayed:
Traceback (most recent call last):
File "main.py", line 71, in <module>
trick_compile(cards)
File "main.py", line 62, in trick_compile
pile1,pile2,pile3 = create_piles(cards)
File "main.py", line 26, in create_piles
pile1.append(cards.pop())
IndexError : pop from empty List
I have concluded that the "empty" list is the list cards, however, I have assigned 21 values to it (which also form the first piles that are displayed). Why is the list empty after they are appended into Pile1, 2 and 3 respectively, and what can one do to fix it.
All help appreciated.
GCSE SPEC FOR PROGRAM: http://filestore.aqa.org.uk/resources/computing/AQA-85203-SNEA3.PDF