简体   繁体   English

我如何询问用户姓名,然后检查用户姓名是否正确?

[英]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 yesno需要是字符串,所以简单地做

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输入函数中,您应该使用+

     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.您将yesno视为对象,您不应该这样做。 You should treat them as string and use "yes" and "no" instead.您应该将它们视为字符串并使用"yes""no"代替。 Plus note that the comparison you are making is case sensitive, so if the user puts "YES" , your code will not print anything.另外请注意,您所做的比较区分大小写,因此如果用户输入"YES" ,您的代码将不会打印任何内容。 You can use .lower() to do a case insensitive comparison:您可以使用.lower()进行不区分大小写的比较:

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

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

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