從邏輯上講,這是有道理的,但不起作用。
dice = 0
while True:
dice = int(input("enter dice: "))
if (dice != 4 or dice != 6 or dice != 8 or dice != 10 or dice != 12 or dice != 20):
print("invalid")
elif (dice == 4 or dice == 6 or dice == 8 or dice == 10 or dice == 12 or dice == 20):
print("valid")
break
print("Accepted dice")
我不斷收到無效的列印陳述句。想要將其作為一個回圈,直到選擇了正確的骰子之一。
uj5u.com熱心網友回復:
使用標志,如評論中提到的,如果您的原始檔案有缺陷......如果您只想修復它,那么您可以將您的 or 更改為 and
dice_is_valid = dice in {4,6,8,10,20}
if dice_is_valid:
...
else:
print("invalid")
uj5u.com熱心網友回復:
問題在于or在這里使用陳述句。以 的輸入為例4。4 != 4如果顯然是假的,但它會評估4 != 6哪個是真的。因此,無論您輸入什么,第一個條件都將始終為真。相反,只需使用這樣的else陳述句
dice = 0
while True:
dice = int(input("enter dice: "))
print("invalid")
if (dice == 4 or dice == 6 or dice == 8 or dice == 10 or dice == 12 or dice == 20):
print("valid")
break
else:
print("invalid")
print("Accepted dice")
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/310960.html
