0

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.

4
  • 2
    The condition in your if doesn't work as you expect. You need to do something like if condition_1 or condition_2 Commented Sep 30, 2019 at 10:36
  • Why not use boolean value for login? Commented Sep 30, 2019 at 10:36
  • @owninggreendragsdude because 1 is a boolean value. Actually all objects in Python have a boolean value, the bool type was a later (and highly debated FWIW) addition. Commented Sep 30, 2019 at 11:38
  • @owninggreendragsdude also the OP may have a need for more than possible 2 values here (I fail to imagine why but we only have a short excerpt of the real code) ;-) Commented Sep 30, 2019 at 11:40

2 Answers 2

2
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...")
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

The condition still won't work as the OP expects (I believe). Please read up on De Morgan's laws. And read the message being printed, it contains the right logic. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.