首页 > 解决方案 > 如何让我的代码跳出循环并实际检测到某个输入

问题描述

我正在对密码库进行基本的 Python 评估,但遇到了我自己似乎无法解决的错误。顺便说一句,伙计们,我不是来这里学习语法和标点符号的,所以如果你只是来编辑我的问题而不提供任何帮助,请不要打扰。

例如,在这部分代码中,我希望用户输入 1 或 2,如果他选择 1,则要求他登录,而如果他选择 2,则要求他注册。但目前它完全忽略了参数并接受任何东西。

另一个问题是,当用户输入有效密码时,它不会因为密码正确而停止,而是出于某种原因重新询问“您的用户名是什么”。

   while True: 
        login_orsignup1 = input('''Press 
1) to Log in
2) to register a new account
''')
        if login_orsignup1 != 1:
            while True: 
                username = input('''What is your,
Username: ''')
                if input_username == username:
                    l_p = input('''What is your password ''')

                    while True:
                        if l_p == input_lockerpassword:
                            print("Password Correct")
                            break
login_originup1()   
----------------------------------------------------------#Full code begins now


l_p = ""
print("------------------------------------------------------------------------")
print('''Welcome to password Locker, a place where you can 
store all your passwords to easily enter your precious accounts without
hassle.''')
print("------------------------------------------------------------------------")
print('''First lets make an account,''')

while True:
    first_name = input('''What is your first name? 
''')
    if first_name.isdigit():  #isdigit, detects if there
        print("Please enter a valid answer, No nubers shoud be present")
    elif first_name == "":
        print("Please enter an answer")
         #the continue code skips the boundries within the loop                                                               and carries on with the connected program until it is succesfully met        

    else:
        break #the break loop exits the current loop and continues with                                                               the next programes following it

while True:
        sur_name = input('''What is your surname?
''')
        if sur_name.isdigit():  #isdigit detects if the
            print("No numbers")
        elif sur_name == "":
            print("Please enter an answer")
             #the continue code skips the boundries within the loop                                                               and carries on with the connected program until it is succesfully met         
        else:
            break

print('''------------------------------------------------------------------------''')
print('''Welcome, {} {} 
what would you like your username to be, it should be something 
memorable and no longer than fifteen characters long, '''.format(first_name, sur_name))
while True:
        input_username = input("")
        if 0 < len(input_username) < 16:
                print('''Nice, username''')
                break
        elif input_username == "":
            print("Please enter an answer")

        else:
            print('''Your username should be a maximum of 15 charecters, ''')
print('''-------------------------------------------------------------------------''')
while True:
    input_lockerpassword = input('''Now it's time to setup a password for your locker, It should be between 4 
and 10 charecters long,  
''')
    if len(input_lockerpassword) > 4 and len(input_lockerpassword) < 11:
        print('''{}, is locked in thanks for joining Password Locker'''.format(input_lockerpassword)) 
        break
    else:
        print("It should be between 4 and 10 charecters long!")
print('''
-------------------------------------------------------------------------------------------''')
def login_originup1():
    print(''' Welcome to password vault, You can either login or create a New account''')
    while True: 
        login_orsignup1 = input('''Press
1) to Log in
2) to register a new account
''')
        if login_orsignup1 != 1:
            while True: 
                username = input('''What is your,
Username: ''')
                if input_username == username:
                    l_p = input('''What is your password ''')

                    while True:
                        if l_p == input_lockerpassword:
                            print("Password Correct")
                            break
login_originup1()```

标签: python

解决方案


问题出在您的login_originup1函数中,您正在制作三个 While 循环,程序无法在您的函数中逃脱,您在if login_orsignup1 != 1 没有 else 语句的情况下询问,所以如果用户想登录,他会按“1”中的输入,然后程序会说那

"1" =! 1是假的

它会寻找一个 else 语句,但找不到一个,所以它会回到循环的开头并要求用户再次输入。这就是第一个循环。

现在,如果用户输入“2”(这意味着用户想要注册),它将让他登录,因为:

"2" =! 1是真的

并将继续到下一个 while 循环,在这里您将询问用户名,用户将提供用户名。现在这是第二个循环

我们现在进入最后一个循环,您要求输入密码,用户将提供密码。程序要么 1. 说它是假的并再次要求输入密码,要么 2. 它将接受密码并打破 While 循环。现在这是第三个循环

那么它为什么要我输入用户名因为 break 语句只中断了它所在的 while 循环,因此 break 语句只中断了第三个 while 循环并返回到第二个循环,第二个循环将使我们回到第三个循环再次

那么如何解决这个问题?

像这样简单:

def login_originup1():
    print('Welcome to password vault, You can either login or create a New account')

    while True: 
        login_orsignu = input('Press\n1) to Log in\n2) to register a new account\n')
        loopBreaker = 0
        if loopBreaker:
            break
        if int(login_orsignu) != 1:
            while True: 
                if loopBreaker:
                    break
                username = input('What is your,\nUsername:')
                if input_username == username:
                    l_p = input('What is your password ')
                    while True:
                        if loopBreaker:
                            break
                        if l_p == input_lockerpassword:
                            print("Password Correct")
                            loopBreaker = 1
                            break


推荐阅读