带验证的名称和年龄代码

时间:2017-09-20 21:29:59

标签: python python-3.x validation

我使用while循环停留在一段代码上。

Namecorrect = ""
Name = input("Please enter your name: ")
while Namecorrect == "":
    NameCorrect = input("Your name is" ,Name, "is that correct? ")
if Namecorrect == "yes":
    Agecorrect = ""
    Age = input("Please enter your age: ")
    while Agecorrect == "":
        Agecorrect = input("Your age is" ,Age, "is that correct? ")
    if Agecorrect == "yes":
        print("Please come in")
    else:
        print ("Please retry")
else:
    print("Please retry")

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

  

问题:它也只是重复了一个问题“你的名字是

你有Indention错误,询问没有条件。 请考虑以下事项:

Namecorrect = None
Agecorrect = None

while True:
    if Namecorrect != 'yes':
        Name = input("Please enter your name: ")
        NameCorrect = input("Your name is {}, is that correct? ".format(Name))

    if Namecorrect == 'yes':
        Age = input("Please enter your age: ")
        Agecorrect = input("Your age is {}, is that correct? ".format(Age))

        if Agecorrect == "yes":
            print("Please come in")
            break

    print("Please retry")