0

In the photo, you can see that line 24 shows up as an error - "pile1" is not defined,"pile2" is not defined, "pile3" is not defined

import random

def create_cards():
  deck = []
  for i in range(21):
    suits = ["♥","♦","♠","♣"]
    numbers = ['1','2','3','4','5','6','7','8','9','10','K','Q','J']
    card = (random.choice(numbers) + random.choice(suits))
    deck.append(card)
  return deck


def create_piles():
  pile1 = []
  pile2 = []
  pile3 = []
  for k in range(7):
    pile1.append(deck.pop())
    pile2.append(deck.pop())
    pile3.append(deck.pop())
  return piles

def user_input():

  print (pile1, pile2, pile3)
  choice = input("Which pile is your card in?")
  if choice == 1:
    deck = 
  elif choice == 2:
    deck = 
  else:
    deck = 

def trick():
  print(0)

deck = create_cards()
piles = create_piles()

How can I fix this (I have tried putting function create_piles within the user_input brackets, still shows up as undefined.)

I. Abbas
  • 1
  • 7

2 Answers2

1

the reason your code isn't working is because your functions are trying to access variables that are not in their scope. Please take the time to read more about scope here: Short Description of the Scoping Rules?

also, you should be indenting with 4 spaces instead of 2

import random

def create_cards():
    deck = []
    for i in range(21):
        suits = ["♥","♦","♠","♣"]
        numbers = ['1','2','3','4','5','6','7','8','9','10','K','Q','J']
        card = (random.choice(numbers) + random.choice(suits))
        deck.append(card)
    return deck

def create_piles(deck):
    pile1 = []
    pile2 = []
    pile3 = []
    for k in range(7):
        pile1.append(deck.pop())
        pile2.append(deck.pop())
        pile3.append(deck.pop())
    return [pile1, pile2, pile3]

def user_input(pile1, pile2, pile3):
    print (pile1, pile2, pile3)

deck = create_cards()
piles = create_piles(deck)
user_input(*piles)
spooky
  • 106
  • 1
  • 5
0

It looks like the easiest fix is to simply change your function to include piles as a parameter. Something like this:

def user_input(piles):
    print(piles)
    choice = input("which pile is your card in?")
Grant Williams
  • 1,469
  • 1
  • 12
  • 24