我的骰子滚动或循环不起作用

时间:2017-05-02 11:29:43

标签: python python-3.x

这是我的代码

import random as r

Diceroll = "Y"
while Diceroll == "Y":
    Roll = r.randrange(1,7)
    print(Roll)
    Diceroll  = input("Would you like to roll again: Y/N")
    while (Diceroll != "Y") or (Diceroll != "N"):
        Diceroll = input("Please enter either Y or N")

print("Thank you for rolling with us")

第一次打印后,它无限循环Would you like to roll again 我确信解决方案很明显,但我找不到它。

3 个答案:

答案 0 :(得分:0)

对于不属于 angular.module('myApp', ['ngMaterial']) .config(function($mdThemingProvider, $provide) { //disable theme generation $mdThemingProvider.generateThemesOnDemand(true); $provide.value('themeProvider', $mdThemingProvider); }) .run(['themeProvider', '$mdTheming', function(themeProvider, $mdTheming) { //create new theme themeProvider.theme('default') .primaryPalette('pink') .accentPalette('orange') .backgroundPalette('yellow'); //reload the theme $mdTheming.generateTheme('default'); //optional - set the default to this new theme themeProvider.setDefaultTheme('default'); }]); Y的输入,您需要更具约束力的条件:

N

while (Diceroll != "Y") and (Diceroll != "N"):
    ...

答案 1 :(得分:0)

一个简单的解决方案是:

import random as r

Diceroll = "Y"
while Diceroll == "Y" and Diceroll != "N":
    Roll = r.randrange(1,7)
    print(Roll)
    Diceroll  = input("Would you like to roll again: Y/N")


print("Thank you for rolling with us")

以下是输出:

4
Would you like to roll again: Y/NY
3
Would you like to roll again: Y/NY
6
Would you like to roll again: Y/NY
4
Would you like to roll again: Y/NY
1
Would you like to roll again: Y/NY
6
Would you like to roll again: Y/NY
6
Would you like to roll again: Y/NY
6
Would you like to roll again: Y/NN
Thank you for rolling with us

答案 2 :(得分:0)

使用错误的logical operator

,您的程序逻辑存在缺陷
  • 逻辑AND运算符:如果两个操作数均为True,则返回True;如果至少一个操作数为False,则返回False

  • 逻辑OR运算符:如果一个或两个操作数为True,则返回True;如果两个操作数均为False,则返回False

这就是while (Diceroll != "Y") or (Diceroll != "N")返回True并输入YN

的原因