这是一个非常奇怪的错误,我不知道发生了什么。基本上,我的代码是最重要的,它还没有完全完成,但无论如何。对于那些玩过顶级王牌的人,我的代码模拟了两手牌的传递。但是,如果您看看第129行(if len(Your_Hand)== 0:
),其中我使用了len()来打印这些手的长度,那么它们相加起来就不多了。Ie 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!!"
几轮之后,我的手有了3个长度,由一张罗恩·韦斯莱的卡片和两张空名单组成:
[('Ron Weasley', 12, 13, 4, 5), [], []]
。我猜你不想有那些空名单。在罪魁祸首似乎是这句话:
它总是在你的手上加上一个空列表。在
您可能想要}。此外,您应该在将碰撞堆重置为
extend
,而不是{[]
之前将碰撞堆添加到胜利者的手上。在其他可能与原始问题无关的观察结果:
if Your_Hand[0][catagory_exchange[choice]] == Opponent_Hand[0][catagory_exchange[choice]]:
行中的if
应该是elif
我重写了你的程序,现在对我有用了。我发现了几件事:
最大的问题是@Kevin在他的答案中指出的问题:你用}从{}复制卡片。在
list.append()
而不是{另外,
category_exchange
包含的值被1关闭。每张卡片中的项目0是一个字符串,选择brains
会导致卡片名称之间的字符串比较。(当我和纳威·隆巴顿连胜时,我非常困惑!)在我重写了移动卡片的代码。我不是先调用
.append()
然后再调用.remove()
,而是调用.pop()
来删除并返回值,并在.append()
中执行该操作。当所有代码都在一行上时,您就不太可能看到有人只更改两行代码中的一行的bug。在另外,我使用了无限循环,在找到可接受的输入时简单地中断,而不是标记变量在找到可接受的输入时发出信号。在
另外,我没有使用标志变量来表示“clash-pile中有卡片”,或者检查卡片列表中的长度为0,而是将其改为直接检查列表值。在
另外,由于我很懒,我让程序接受一个类别的数字。不需要输入
knowledge
,只需输入2
就可以了。在如果你有任何问题请告诉我。在
相关问题 更多 >
编程相关推荐