[英]If-elif-else statement not working in simple Python 3.3 class?
My code is: 我的代码是:
def nameAndConfirm():
global name,confirm
print("What is your name? ")
name = input()
str(name)
print("Is",name,"correct? ")
confirm = input()
str(confirm)
print(confirm)
if confirm.upper() == "Y" or "YES":
classSelection()
elif confirm.upper() == "N" or "NO":
nameAndConfirm()
else:
print("Valid answers are Y/Yes or N/No!")
nameAndConfirm()
nameAndConfirm()
Critique on this code would be nice as well. 对这段代码的批评也会很好。 I know its very shifty, I know how to make it shorter in some ways but I was trying to get my if-elif-else to work.
我知道它非常狡猾,我知道如何在某些方面缩短它,但我试图让我的if-elif-else工作。 I have no clue what else I can do as I've tried everything I know.
我不知道我能做什么,因为我已经尝试了所有我知道的事情。 Also I made an indent 4 spaces in the above code.
我也在上面的代码中缩进了4个空格。 **Edit: sorry the error is that it always runs the "if" it never goes past the first if line no matter what you enter in for confirm
**编辑:抱歉,错误是它始终运行“if”它永远不会超过第一个if行,无论你输入什么来确认
The condition confirm.upper() == "Y" or "YES"
and the other one are not evaluated as you expect. 条件
confirm.upper() == "Y" or "YES"
,另一个未按预期进行评估。 You want 你要
confirm.upper() in {"Y", "YES"}
or 要么
confirm.upper() == "Y" or confirm.upper() == "YES"
Your condition is equivalent to: 你的病情相当于:
(confirm.upper() == "Y") or "YES"
which is always truthy: 总是真的:
In [1]: True or "Yes"
Out[1]: True
In [2]: False or "Yes"
Out[2]: 'Yes'
On a separate note, the lines 在单独的注释,行
str(name)
and 和
str(confirm)
don't do anything. 什么都不做 The values returned by the functions are not saved anywhere, and
name
and confirm
are not altered. 函数返回的值不会保存在任何位置,并且不会更改
name
和confirm
。 Moreover, they are already strings to begin with, because they hold the return values of input()
. 而且,它们已经是字符串开头,因为它们保存
input()
的返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.