-1
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

I. Abbas
  • 3
  • 1
  • 2
    You're calling `create_piles` 3 times. After the first call, `cards` is empty. Maybe you want to pass a copy of `cards` to `create_piles`? – pault Apr 25 '18 at 15:49
  • AttributeError: 'NoneType' object has no attribute 'pop' || I passed a copy of cards to create_piles both in the function declaration and in trick_Compile – I. Abbas Apr 25 '18 at 15:52
  • 1
    You're also assigning `cards = reassemble(choice,pile1,pile2,pile3)`, but the `reassemble` function doesn't have a return value. – Bill the Lizard Apr 25 '18 at 15:52
  • Read [this answer](https://stackoverflow.com/a/986145/5858851). After that, try using some print statements to debug. You're very close to having it all working. – pault Apr 25 '18 at 15:53

1 Answers1

0

I spotted few errors in your code

in create_piles, you were poping from cards whereas the parameter you passed was deck. Here is the corrected version

def create_piles(deck):
    pile1 = []
    pile2 = []
    pile3 = []
    for i in range(7):
        pile1.append(deck.pop())
        pile2.append(deck.pop())
        pile3.append(deck.pop())
    print("\n" *3)
    print(pile1)
    print(pile2)
    print(pile3)
    return pile1,pile2,pile3

you are also trying to print cards in user_choice whereas cards is not global, that gives empty list.

in reassemble, you created cards without returning it

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
  return cards