簡體   English   中英

我如何詢問用戶姓名,然后檢查用戶姓名是否正確?

[英]How do I ask user for name then check if it is correct with the user?

我如何詢問用戶姓名,然后檢查用戶姓名是否正確? 我嘗試了很多不同的方法來做到這一點,但這是我做過的最好的方法:

Name = input("What Is Your Name: ")
Answer = input("Name Inputted Is", Name, "Is This Correct(enter yes or no): ")
if Answer == yes:
    print("good thank you")
if Answer == no:
    print("Please Reset Your Name")
        Name = input("What Is Your Name: ")
        Answer = input("Name Inputed Is", Name, "(there is not another reset)")

這不起作用,請讓我知道出了什么問題。

yesno需要是字符串,所以簡單地做

if Answer == "yes":

if Answer == "no"

您的代碼有兩處錯誤:

  1. 您正在使用逗號,在定義Answer輸入函數中,您應該使用+

     Answer = input("Name Inputted Is " + Name + " Is This Correct(enter yes or no): ")
  2. 您將yesno視為對象,您不應該這樣做。 您應該將它們視為字符串並使用"yes""no"代替。 另外請注意,您所做的比較區分大小寫,因此如果用戶輸入"YES" ,您的代碼將不會打印任何內容。 您可以使用.lower()進行不區分大小寫的比較:

     if Answer.lower() == "yes".lower(): print("good thank you")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM