Login system with number of tries on python how can i simplify it?

Advertisements

I am a Beginner in Python and i made this login system with number of tries. I think it can be simplified Can anyone help?

a=int(input("Enter the Password: "))
i=5
if a==1234:
        print("ACCESS GRANTED")
        
while not a==1234:
    print(f"INVALID PASSWORD ( {i} times left)")
    a=int(input("Enter the Password: "))
    i-=1
    if a==1234:
        print("ACCESS GRANTED")
    if i==0:
        print("Console has been locked")
        break

I tried it to change the number of print("ACCESS GRANTED") but I dont get how to without doing it wrong.

>Solution :

Maybe something like this:

a = 0
i = 6
while not a==1234:
    a=int(input("Enter the Password: "))
    i-=1
    if a==1234:
        print("ACCESS GRANTED")
    elif i==0:
        print("Console has been locked")
        break
    else:
        print(f"INVALID PASSWORD ( {i} times left)")

Leave a ReplyCancel reply