Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2017年GCSE卡戏法:“;“从空列表中弹出”;索引器_Python - Fatal编程技术网

Python 2017年GCSE卡戏法:“;“从空列表中弹出”;索引器

Python 2017年GCSE卡戏法:“;“从空列表中弹出”;索引器,python,Python,我是GCSE计算机科学专业的学生。这段代码是基于去年的课程作业,其中要求一个纸牌戏法 运行脚本时,将显示以下错误: 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

我是GCSE计算机科学专业的学生。这段代码是基于去年的课程作业,其中要求一个纸牌戏法

运行脚本时,将显示以下错误:

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)
回溯(最近一次呼叫最后一次):
文件“main.py”,第71行,在
技巧(卡片)
文件“main.py”,第62行,在trick_编译中
桩1、桩2、桩3=创建桩(卡片)
文件“main.py”,第26行,在create_中
pile1.append(cards.pop())
索引器:从空列表中弹出
我的结论是“空”列表是列表卡,但是,我给它分配了21个值(这也构成了显示的第一堆)。为什么列表在分别附加到Pile1、2和3之后是空的,以及如何修复它

谢谢你的帮助

GCSE课程规范:

我在您的代码中发现了一些错误

在create_piles中,您是从卡片中弹出的,而您传递的参数是deck。这是正确的版本

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
您还试图在用户选择中打印卡片,而卡片不是全局的,这会给出空列表

在“重新组装”中,您创建了卡片,但没有返回

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

您正在呼叫
create\u piles
3次。第一次调用后,
为空。也许您想将一份
卡片的副本传递给
创建堆
?AttributeError:“非类型”对象没有属性“pop”| |我在函数声明和技巧编译中都传递了一份卡片的副本来创建堆您也在分配
卡片=重新组装(choice,pile1,pile2,pile3)
,但是
重新组装
函数没有返回值。读取。之后,尝试使用一些print语句进行调试。你很快就可以让一切正常运转了。
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