146k views
0 votes
Write a program that will ask the user for their password. If they get it wrong ( compared to some password they already have stored somewhere) print "sorry", try again" and ask them to enter their password again. If they get it wrong 5 times, the program should stop asking them and print "account locked". Once they get it right, print "access granted".

User Kuryaki
by
5.6k points

1 Answer

7 votes

In python 3:

user_password = "password"

guesses = 5

while True:

attempt = input("What's your password? ")

if attempt == user_password:

print("access granted")

break

print("sorry, try again")

guesses -= 1

if guesses == 0:

print("account locked")

break

I hope this helps!

User CHawk
by
6.3k points