重新启动程序如果用户输入为否

时间:2018-11-12 04:06:01

标签: python

如果密码不正确,我希望程序向用户询问密码。我该怎么办?

#program that saves user passwords

user_input = ""

FB = input("what is your facebook pasword \n")
print("your facebook passoerd is " + FB  + " is this correct?")

user_input = input()
if (user_input == "yes"): 
    print("password has been saved")
elif user_input == "no":
    print("password was not saved")
else:
    print("i do not understand. sorry")

1 个答案:

答案 0 :(得分:0)

欢迎使用StackOverflow!

通常的技巧之一是将所有内容包装在while循环中,如果用户输入的内容不是"no"则中断循环。

while True:
    user_input = ""

    FB = input("what is your facebook pasword \n")
    print("your facebook password is " + FB  + " is this correct?")

    user_input = input()
    if user_input is "yes": 
        print("password has been saved")
        break #add break here to exit the program
    elif user_input is "no":
        print("password was not saved")
    else:
        print("i do not understand. sorry")
        break #add break here to exit the program