簡體   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