简体   繁体   中英

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

How do I ask user for name then check if it is correct with the user? I have tried a lot of different ways to do this but this one has been the best one I have done:

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

This dose not work please let me know what is wrong.

yes and no need to be strings as well so simply do

if Answer == "yes":

And

if Answer == "no"

There are two things wrong with your code:

  1. You are using comma , in your input function for defining Answer , you should use + :

     Answer = input("Name Inputted Is " + Name + " Is This Correct(enter yes or no): ")
  2. You are treating yes and no as objects, you should not do this. You should treat them as string and use "yes" and "no" instead. Plus note that the comparison you are making is case sensitive, so if the user puts "YES" , your code will not print anything. You can use .lower() to do a case insensitive comparison:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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