我正在编写一个类似yahtzee的游戏,其中玩家掷5个骰子并选择重掷哪个骰子。Python:在列表中迭代true和false变量,结果不同
我不能让我的函数正确迭代用户输入验证它们是有效的。
下面是一些代码:
def diceroll():
raw_input("Press enter to roll dice: ")
a = random.randint(1, 6)
b = random.randint(1, 6)
c = random.randint(1, 6)
d = random.randint(1, 6)
e = random.randint(1, 6)
myroll.append(a)
myroll.append(b)
myroll.append(c)
myroll.append(d)
myroll.append(e)
print "Your roll:"
print myroll
diceSelect()
def diceSelect():
s = raw_input("Enter the numbers of the dice you'd like to roll again, separated by spaces, then press ENTER: ")
rollAgain = map(int, s.split())
updateMyRoll(rollAgain)
def updateMyRoll(a):
reroll = []
for n in a:
if n in myroll:
reroll.append(n)
removeCommonElements(myroll, a)
print "deleting elements..."
elif n not in myroll:
print "I don't think you rolled", n, "."
diceSelect()
else:
print "I don't understand..."
diceSelect()
print "Your remaining dice: ", myroll
def removeCommonElements(a,b,):
for e in a[:]:
if e in b:
a.remove(e)
b.remove(e)
的问题很可能在diceSelect功能,这样我可以只输入真值,它工作正常,我只能输入错误的值,并获得唯一想要的效果第一个错误的值(我知道基于代码...但希望改变),或者我可以输入true和false值,但它只会影响真实值,但会忽略错误值。
如何迭代这些值并重新提示用户输入所有真值?
什么是“预期效果”? – Carcigenicate
@Carcigenicate我很抱歉不清楚,我会更新原文。我希望它能够验证来自用户的所有值匹配实际在原始5个骰子中滚动的值。如果他们不是,我希望它回到提示他们输入有效值。这是当我只输入虚假值时发生的行为,但是,它只会告诉用户'我认为你只输入了第一个虚假值而不是x'。 – Hanzy
是否有任何理由让你用Python2学习Python?这些天你应该使用Python3 –