即使输入正确,我的while循环也不会退出

时间:2019-11-20 18:02:55

标签: python

即使我输入y,y,n或no,我的while循环仍继续进行。

import random

def rollDice():
    diceRoll = random.randint(1,6)
    return diceRoll

def reRollDie1():
    reRollDie1 = input("Would you like to re roll die 1?")
    reRollDie1.lower()

    while reRollDie1 != "yes" or reRollDie1 != "y" or reRollDie1 != "no" or reRollDie1 != "n":
        reRollDie1 = input("Sorry that answer is invalid please try again. Would you like to re-roll die 1? ")         
reRollDie1()

输出:

  

很抱歉,答案无效。请重试。您想重新滚动死1吗?否

     

很抱歉,答案无效。请重试。您想重新滚动死1吗?是的

     

很抱歉,答案无效。请重试。您想重新滚动死1吗?是的

1 个答案:

答案 0 :(得分:3)

您正在使用or检查所有选项。由于每个都是不平等测试,并且每个选项都不同,因此条件始终为真。相反,我建议使用诸如reRollDie1 not in {"yes", "y", "no", "n"}之类的测试。