我正在編寫一個類似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 –