Steve
Steve

Reputation: 19

How do i fix my while loop

I am writing a program that asks the user to enter a password. If the password matches the constant I've set it prints out a "successfully logged in message". However if the password is incorrect, it gives the number of guesses left and asks the user to try again. The program should end after 3 incorrect guesses but it keeps on asking even after 3 attempts have been used. I think the problem is in my while loop but I am unsure.

Code:

def main():
    PASSWORD = "apple"
    ALLOWED = 3

    password = input("Enter the password: ")
    while password != PASSWORD :
        ALLOWED = ALLOWED - 1
        print("Wrong. You have", ALLOWED, "guesses left")

        if ALLOWED == 0:
                    print("You have been locked out")
        password = input("Enter again ")            


    print("You have successfully logged into the system")



main()

Upvotes: 0

Views: 115

Answers (6)

C. McCarthy
C. McCarthy

Reputation: 102

Managed to make it work but just making it check if ALLOWED == 0 and if it does print "you are locked out", and if ALLOWED <= 0 then it will not let you go any further.

def main():
    PASSWORD = "apple"
    ALLOWED = 3

    password = input("Enter the password: ")
    while password != PASSWORD or ALLOWED <= 0:
        ALLOWED = ALLOWED - 1
        if ALLOWED > 0: print("Wrong. You have", ALLOWED, "guesses left")
        if ALLOWED == 0: print("You have been locked out") 
        if ALLOWED < 0:
                    print("You have been locked out")
        password = input("Enter again ")            


    print("You have successfully logged into the system")



main()

also wrote a different version which seemed easier in my opinion

def main():

    USERNAME = "admin"
    PASSWORD = "root"
    ATTEMPTS = 3
    while ATTEMPTS >= 1:
        print("You have",ATTEMPTS,"attempts left")
        if ATTEMPTS == 0:
            break
        user = input("Please enter your username:")
        password = input("Now enter your password:")
        if user == USERNAME and password == PASSWORD:
            print("\nYou have successfully logged into the system")
            return
        else:
            print("\nThis user name or password does not exist\n")
            ATTEMPTS -= 1
    print("you have been locked out.")

main()

Upvotes: 0

Darcinon
Darcinon

Reputation: 161

Your while loop requires a correct password to finish, and there is no other way to exit the loop. I suggest a break statement:

def main():
    PASSWORD = "apple"
    ALLOWED = 3

    password = input("Enter the password: ")
    while password != PASSWORD :
        ALLOWED = ALLOWED - 1
        print("Wrong. You have", ALLOWED, "guesses left")

        if ALLOWED == 0:
            print("You have been locked out")
            break
        password = input("Enter again ")

    print("You have successfully logged into the system")

You may want to do more research if your program needs to be secure.

Upvotes: 0

ShadowRanger
ShadowRanger

Reputation: 155323

Change print("You have been locked out") to sys.exit("You have been locked out") (or otherwise exit the main). Remember to import sys to use sys.exit.

Upvotes: 0

Llamageddon
Llamageddon

Reputation: 3526

You need to use break to exit the loop, or add a secondary condition, otherwise it will keep going anyway until the password is correct.

So, either:

while (password != PASSWORD) and (ALLOWED > 0):

Or:

    if ALLOWED == 0:
                print("You have been locked out")
                break

Upvotes: 1

TheoretiCAL
TheoretiCAL

Reputation: 20561

Right now you never exit your while loop. To break out of it, use the break keyword. To exit your program completely, you will need to import sys and sys.exit() I suggest adding these to your if ALLOWED == 0 statement.

Upvotes: 3

IamK
IamK

Reputation: 2974

The condition password != PASSWORD is not enough to exit the loop (It will exit the loop only if you give the right password). Add the condition also in while (password != PASSWORD and ALLOWED > 0)

Upvotes: 0

Related Questions