如何在python中重新启动else

2024-04-25 12:33:58 发布

您现在位置:Python中文网/ 问答频道 /正文

while True:
    print ('What is your name')
    name = input()
    if name == 'Joe':
        continue
    else:
        break
    print ('What is the password')
    password = input()
    if password == '123':
        break
print ('Permission Granted')

每当我键入乔以外的内容时,它就会把我带到最后一行。我是编程新手,因此任何帮助都将不胜感激。你知道吗


Tags: thenametrueinputyourifispassword
3条回答

如果我理解正确的话,那就是你的代码逻辑错了。尝试:

while True:
    print ('What is your name')
    name = input()
    if name != 'Joe':
        continue
    print ('What is the password')
    password = input()
    if password == '123':
        break
print ('Permission Granted')

听起来你想一直问一个名字,直到“乔”被输入。一旦你得到你想要的名字,检查密码。在这种情况下,请尝试:

while True:
    name = input('What is your name? ')
    if name != 'Joe':
        continue
    password = input('What is the password? ')
    if password == '123':
        print('Permission granted')
        break

也许这有帮助?你知道吗

   while True:
        print ('What is your name')
        name = input()
        if name != 'Joe': 
            continue  # if the name is not equal to 'Joe', go to the beginning of the loop     
        print ('What is the password')
        password = input()
        if password == '123':
            print ('Permission Granted')  # print this if the password is correct
            break  # if the password equals '123', exit the while loop

相关问题 更多 >