这是一个非常奇怪的错误,我不知道发生了什么。基本上我的代码是顶级王牌,但尚未完全完成,但无论如何。对于那些玩过顶级王牌的人来说,我的代码模拟了双手之间的牌传递。但是,如果你看一下第129行(if len(Your_Hand)== 0:
),我用len()来打印这些牌的长度,他们就不会加起来。也就是len()说一个读取列表的长度显然不对?
谢谢,如果你能看看并尝试帮助我,那将是什么样的辉煌!
这是我的代码:
#top trumps stuff
import random
import time
pack = [("harry Potter",2,3,4,5),
("Hermione Granger", 5,6,7,8),
("Ron Weasley", 12, 13,4,5),
("Neville Longbottom", 1,1,1,1),
("Ginny Weasley",2,3,4,5),
("Draco Malfoy",3,6,7,8) ]
pile_1 = []
pile_2 = []
clash_pile = []
Your_Hand = 0
Opponent_Hand = 0
end_loop=0
catagory_exchange = {"brains":0,
"knowledge": 1 ,
"cunning": 2,
"evil":3}
choice = 0
acceptable_answers = catagory_exchange.keys()
proceed = 0
clash = 0
end_game = 0
#Shuffles the pack
random.shuffle(pack)
# Deals the cards
Dealer_count =0
while Dealer_count <len(pack):
pile_1.append(pack[0])
pack.remove(pack[0])
pile_2.append(pack[0])
pack.remove(pack[0])
#Asks Which pile they'd like
Which_Pile = raw_input(">> Which Pile would you like 1 or 2?")
while end_loop != 1:
if Which_Pile == "1":
Your_Hand = pile_1
Opponent_Hand = pile_2
print "Lets now start!"
end_loop = 1
elif Which_Pile == "2":
Your_Hand = pile_2
Opponent_Hand = pile_1
print "Lets now start!"
end_loop = 1
else:
Which_Pile = raw_input("You didn't pick 1 or 2! Pick again!!")
while end_game != 1:
print len(Your_Hand)
print Your_Hand
print len(Opponent_Hand)
print Opponent_Hand
print Your_Hand[0]
choice = raw_input(">> choose a catagory, brains, knowledge, cunning, evil - ")
if choice in acceptable_answers:
proceed = 1
else:
print "Thats not an catagory"
if proceed == 1:
if Your_Hand[0][catagory_exchange[choice]] > Opponent_Hand[0][catagory_exchange[choice]]:
if clash == 1:
print "You won the cards from previous rounds aswell!!"
clash = 0
clash_pile[:] = []
else:
print "You won the card you received %s" % Opponent_Hand[0][0]
Your_Hand.append(Opponent_Hand[0])
Your_Hand.append(clash_pile)
Opponent_Hand.remove(Opponent_Hand[0])
Your_Hand.append(Your_Hand[0])
Your_Hand.remove(Your_Hand[0])
elif Your_Hand[0][catagory_exchange[choice]] < Opponent_Hand[0][catagory_exchange[choice]]:
if clash ==1:
print "You lost the cards from previous rounds aswell"
clash = 0
clash_pile[:] = []
else:
print "You lost the card"
Opponent_Hand.append(Your_Hand[0])
Your_Hand.append(clash_pile)
Your_Hand.remove(Your_Hand[0])
Opponent_Hand.append(Opponent_Hand[0])
Opponent_Hand.remove(Opponent_Hand[0])
if Your_Hand[0][catagory_exchange[choice]] == Opponent_Hand[0][catagory_exchange[choice]]:
clash =1
print "They both have the same value"
print "They have been added to a pile which you will win when you win the next round"
clash_pile.append(Your_Hand[0])
clash_pile.append(Opponent_Hand[0])
Opponent_Hand.remove(Opponent_Hand[0])
Your_Hand.remove(Your_Hand[0])
if len(Your_Hand)== 0:
print "Oh no! You have run out of cards! You lose!!"
if len(Opponent_Hand)==0:
print "Well done! Your opponent has run out of cards! You win!!"
答案 0 :(得分:4)
经过几轮后,我的手长度为3,由一张Ron Weasley卡和两张空列表组成:[('Ron Weasley', 12, 13, 4, 5), [], []]
。我猜你不想在那里有那些空名单。
罪魁祸首似乎是这一行:
Your_Hand.append(clash_pile)
总是会向您的手中添加一个空列表。
你可能想要extend
而不是append
。此外,您应该在将碰撞桩重置为[]
之前将碰撞桩添加到获胜者的手中。
其他可能与您原始问题无关的观察结果:
if
中的if Your_Hand[0][catagory_exchange[choice]] == Opponent_Hand[0][catagory_exchange[choice]]:
应该是elif
答案 1 :(得分:1)
我重写了你的程序,它现在适用于我。我发现了几件事:
最大的问题是@Kevin在回答中提到的问题:您使用list.append()
代替list.extend()
来复制来自clash_pile
的卡片。
此外,category_exchange
包含一个关闭的值。每张卡中的第0项是一个字符串,选择brains
导致卡名称之间的字符串比较。 (当我和Neville Longbottom一起赢得胜利时,我感到非常困惑!)
我重写了移动卡片的代码。我没有先调用.append()
然后调用.remove()
,而是调用.pop()
删除值并将其返回,并在.append()
内执行此操作。当它全部在一行上时,你不太可能看到有人改变两行代码中的一行的错误。
此外,当找到可接受的输入时,我不是使用标志变量来发信号,而是使用无限循环,只是在找到可接受的输入时断开。
此外,我没有使用标志变量来表示“冲突堆中有卡片”,或者在卡片列表上检查长度为0,而是将其更改为直接检查列表值。
另外,由于我很懒,我让程序接受一个类别的数字。
knowledge,而不是输入2
。
如果您有任何问题,请与我们联系。
#top trumps stuff
import random
import time
pack = [
("Harry Potter",2,3,4,5),
("Hermione Granger", 5,6,7,8),
("Ron Weasley", 12, 13,4,5),
("Neville Longbottom", 1,1,1,1),
("Ginny Weasley",2,3,4,5),
("Draco Malfoy",3,6,7,8)
]
pile_1 = []
pile_2 = []
clash_pile = []
category_exchange = {
"brains" : 1,
"knowledge" : 2 ,
"cunning" : 3,
"evil" : 4,
}
#Shuffles the pack
random.shuffle(pack)
# Deals the cards
while pack:
pile_1.append(pack.pop(0))
pile_2.append(pack.pop(0))
#Asks Which pile they'd like
while True:
Which_Pile = raw_input(">> Which Pile would you like 1 or 2?")
if Which_Pile.strip() == "1":
Your_Hand = pile_1
Opponent_Hand = pile_2
print "Lets now start!"
break
elif Which_Pile.strip() == "2":
Your_Hand = pile_2
Opponent_Hand = pile_1
print "Lets now start!"
break
else:
print "You didn't pick 1 or 2! Pick again!!"
while True:
if not Your_Hand:
print "Oh no! You have run out of cards! You lose!!"
break
if not Opponent_Hand:
print "Well done! Your opponent has run out of cards! You win!!"
break
print "\n"
print "DEBUG: Opponent: len: %d top_card: %s" % (len(Opponent_Hand), str(Opponent_Hand[0]))
print "DEBUG: You: len: %d top_card: %s" % (len(Your_Hand), str(Your_Hand[0]))
print "Here is your top card: '%s'" % str(Your_Hand[0])
while True:
choice = raw_input(">> choose a category, brains, knowledge, cunning, evil: ")
if choice in category_exchange:
i = category_exchange[choice]
break
else:
try:
i = int(choice)
except ValueError:
pass
if i in category_exchange.values():
break
print "That's not a category"
print "Opponent had: %s" % str(Opponent_Hand[0])
if Your_Hand[0][i] > Opponent_Hand[0][i]:
if clash_pile:
print "You won the cards from previous rounds as well!!"
else:
print "You won the card!"
Your_Hand.append(Opponent_Hand.pop(0))
Your_Hand.extend(clash_pile)
clash_pile = []
Your_Hand.append(Your_Hand.pop(0))
elif Your_Hand[0][i] < Opponent_Hand[0][i]:
if clash_pile:
print "You lost the cards from previous rounds as well"
else:
print "You lost the card!"
Opponent_Hand.append(Your_Hand.pop(0))
Opponent_Hand.extend(clash_pile)
clash_pile = []
Opponent_Hand.append(Opponent_Hand.pop(0))
else:
assert Your_Hand[0][i] == Opponent_Hand[0][i]
print "Both cards have the same value."
print "They have been added to a pile which will go to the next winner."
clash_pile.append(Your_Hand.pop(0))
clash_pile.append(Opponent_Hand.pop(0))