Python If语句和逻辑运算符问题 [英] Python If statement and logical operator issue
本文介绍了Python If语句和逻辑运算符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个小型python程序来模拟滚动单个骰子。我的问题出现在我希望程序接受大写和小写'Y'以触发重新滚动。当我接受大写或小写y但我尝试更改行时,我的程序有效
I'm trying to create a small python program to emulate rolling a single die. My problem arises in that I want the program to accept both upper and lower-case 'Y' to trigger a re-roll. My program works when I accept either upper or lower case y but when I try to change the line
if usr == 'Y'
到
if usr == 'Y' or 'y'
会产生问题永远不要输入else语句,使程序永远不会终止。
creates a problem where it never enters the else statement, making the program never terminate.
import random
def DiceRoll():
number = random.randrange(1, 7, 1)
print("Dice Roll: ", number)
AskUser()
return
def AskUser():
usr = input("Roll Again? (Y/N) \n")
if usr == 'Y':
DiceRoll()
else :
print("Thank you for playing!")
return
DiceRoll()
推荐答案
你必须
if usr == 'Y' or usr == 'y'.
目前正在评估语句'y'作为自己的条件,在这种情况下'y'总是评估为真。
It is currently evaluating the statement 'y' as its own condition, and in this case 'y' always evaluates to true.
这篇关于Python If语句和逻辑运算符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文