Kingline 5858
Kingline 5858

Reputation: 9

My while loop keeps looping keeps repeating even when the user inputs the correct information

I know the site has some answered strings about my query but they don't help. I am making this program where the admin knows the password and when asked for the password if the password is wrong it will ask them to enter the password until they get it correct.

admin_password = input("To view a user's details, enter the admin pasword: ")
while True:
    if admin_password != "AdminLogin":
        print("Incorrect password")
    else:
        break

Upvotes: -1

Views: 1696

Answers (2)

Ridowan Ahmed
Ridowan Ahmed

Reputation: 95

Your loop seems fine. I think your problem is in taking input. Are you running a Python 3 code with a Python 2 interpreter? See this: input string in python 3. By the way, you can use this piece of code. Works fine by me.

admin_password = raw_input("To view a user's details, enter the admin pasword: ")

while admin_password != "AdminLogin":
    admin_password = raw_input("To view a user's details, enter the admin pasword: ")

print "Logged In"

Upvotes: 0

ADug
ADug

Reputation: 314

Move the admin_password to inside the loop

while True:
    admin_password = input("To view a user's details, enter the admin 
pasword: ")
    if admin_password != "AdminLogin":
        print("Incorrect password")
    else:
        break

Right now you are taking admin_password once and are repeatedly checking it against the string.

Upvotes: 1

Related Questions