我试图循环字符串“输入密码”,直到条件密码== Hello是满意的。然而,目前这段代码将转移到询问用户“名称”上,而不管输入的密码是什么。
password = None
while password != 'Hello':
print("Enter Password")
password = input()
if password == 'Hello':
print("Access Granted, Welcome to second of the progam!")
name = input("Please enter your name : ")
if name == 'Jared':
print("Wow, a truly spectaular name!")
elif name == 'Madonna':
print("May I have your autograph, please?")
elif name == 'Cher':
print("May I have your autograph, please?")
else:
print(name, ", That's a nice name.")
发布于 2021-12-15 17:11:23
您没有正确地缩进if语句。此外,没有必要检查密码两次!如果您运行它,它将按需要工作:)
password = ''
name = ''
while password != 'hello':
print("Enter Password")
password = input()
else:
print ("Access Granted, Welcome to second of the program!")
name = input("Please enter your name : ")
if name == 'Jared':
print("Wow, a truly spectaular name!")
elif name == 'Madonna':
print("May I have your autograph, please?")
elif name == 'Cher':
print("May I have your autograph, please?")
else:
print(name, ", That's a nice name.")
希望这会有帮助!)
发布于 2021-12-15 17:01:07
您不需要检查密码是否正确两次( while
条件和if
语句)。使用无限循环,然后在密码正确时将其断开。
您还可以将密码提示符作为input()
的参数。
while True:
password = input("Enter Password: ")
if password == 'Hello':
print("Access Granted, Welcome to second of the progam!")
break
else:
print("Incorrect password, try again")
发布于 2021-12-15 17:16:56
问题在于,如果密码是“hello”程序将不会execute.so,程序的控制移到循环的下一行,则会出现will循环中的条件。查看自己的while循环条件,并根据需要更改它。
https://stackoverflow.com/questions/70367486
复制相似问题