[英]How do I ask for an input from a user and check that value within a range to give me the correct response
[英]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)")
這不起作用,請讓我知道出了什么問題。
yes
, no
需要是字符串,所以簡單地做
if Answer == "yes":
和
if Answer == "no"
您的代碼有兩處錯誤:
您正在使用逗號,
在定義Answer
輸入函數中,您應該使用+
:
Answer = input("Name Inputted Is " + Name + " Is This Correct(enter yes or no): ")
您將yes
和no
視為對象,您不應該這樣做。 您應該將它們視為字符串並使用"yes"
和"no"
代替。 另外請注意,您所做的比較區分大小寫,因此如果用戶輸入"YES"
,您的代碼將不會打印任何內容。 您可以使用.lower()
進行不區分大小寫的比較:
if Answer.lower() == "yes".lower(): print("good thank you")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.