陷入while循环

时间:2016-10-23 21:46:42

标签: python loops while-loop numbers int

我正在为我的侄子做一个骰子,所以他可以在没有骰子的情况下玩桌面游戏。即使我输入了一个真实的条件,我也陷入了一段时间。我试图让他选择他想要使用哪个骰子,但如果他选择了一个错误的骰子,那么它会要求他再次输入它。我的代码是......

dice_select = input('Enter the amount of sides are on the dice you want to throw, either 4, 6, or 12:  ')
while dice_select != 4 or dice_select != 6 or dice_select != 12:
    dice_select = int(input('Sorry thats not quite right. Enter the amount of sides are on the dice you want to throw, either 4, 6, or 12:  '))

如果我输入4,6或12,那么当它应该继续时,它仍会让我进入循环。

1 个答案:

答案 0 :(得分:2)

试试这个:

    dice_select = int(input('Enter the amount of sides are on the dice you want to throw, either 4, 6, or 12:  '))
    while not (dice_select == 4 or dice_select == 6 or dice_select == 12):
        dice_select = int(input('Sorry thats not quite right. Enter the amount of sides are on the dice you want to throw, either 4, 6, or 12:  '))

这意味着循环直到dice_select等于4,6或12。

使用您的程序,您的while循环条件始终为true,因为当它等于4时,您的程序将检查false或true或true,这将始终为true。

换句话说,当dice_select同时等于4,6和12时,你的程序正在寻找。这是不可能的。