繁体   English   中英

if-elif-else语句不能用于简单的Python 3.3类?

[英]If-elif-else statement not working in simple Python 3.3 class?

我的代码是:

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()

对这段代码的批评也会很好。 我知道它非常狡猾,我知道如何在某些方面缩短它,但我试图让我的if-elif-else工作。 我不知道我能做什么,因为我已经尝试了所有我知道的事情。 我也在上面的代码中缩进了4个空格。 **编辑:抱歉,错误是它始终运行“if”它永远不会超过第一个if行,无论你输入什么来确认

条件confirm.upper() == "Y" or "YES" ,另一个未按预期进行评估。 你要

confirm.upper() in {"Y", "YES"}

要么

confirm.upper() == "Y" or confirm.upper() == "YES"

你的病情相当于:

(confirm.upper() == "Y") or "YES"

总是真的:

In [1]: True or "Yes"
Out[1]: True

In [2]: False or "Yes"
Out[2]: 'Yes'

在单独的注释,行

str(name)

str(confirm)

什么都不做 函数返回的值不会保存在任何位置,并且不会更改nameconfirm 而且,它们已经是字符串开头,因为它们保存input()的返回值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM