简体   繁体   中英

While loop not looping - Python

A while loop in my code is not looping.

I've tried ensuring the while loop would loop but no loop was executed.

#Name_Input
login = 1
while login == 1:
    print("Enter the username and password \n")
    username = input("Username: ")
    password = input("Password: ")
    if password and username != "cameron" and "123":
        print("\nWrong username or password... \nTry Again...")
        login = 1

I am expecting the loop to go back to the login when the login details are entered incorrectly.

login = 1
while login == 1:
    print("Enter the username and password \n")
    username = str(input("Username: "))
    password = str(input("Password: "))
    if username  ==  "cameron" and   password == "123":
        login=2
    else:
        print("\nWrong username or password... \nTry Again...")

Try this if syntax:

login = 1
while login == 1:
    print("Enter the username and password \n")
    username = input("Username: ")
    password = input("Password: ")
    if password !=  "123" or username != "cameron":
        print("\nWrong username or password... \nTry Again...")
        login = 1
    else:
        login = -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM