2016-10-23 138 views
0

我想为我的侄子做一个骰子tosser,所以他可以玩没有骰子的棋盘游戏。即使我输入了一个真实的条件,我也陷入了一个while循环。我试图让他选择他想用的骰子,但是如果他选择了一个错误的骰子,那么它会要求他再次输入。我的代码是...卡在while循环

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,那么当它应该继续时,它仍然使我进入循环。

+0

条件可能是错误的唯一方法是如果'dice_select'等于4,6和12全部在同一时间。 –

+0

重新阅读你的状况。评估它的价值,你寻求。当'dice_select'是'4'时会发生什么? – njzk2

回答

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循环的条件永远是正确的,因为当它等于4,你的程序正在检查错误或真实或真实,这将永远是真实的。

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

+0

嗨,感谢您的回复。我现在得到这个 – Lomore

+0

输入你想投掷的骰子的数量,无论是4,6还是12:12 对不起,这不太对。输入你想投掷的骰子的数量,无论是4,6还是12:12 你选择掷出12面骰子,结果是1 – Lomore

+0

因为在你的第一个陈述中,你不会将你的输入转换成一个整数,然后你会比较一个字符串和一个整数,这在python中总是为false。我将编辑我的上述代码以反映这一点。 – creeperdomain