Q.To secure your account, whether it be an email, online bank account or any other account, it is important that we use authentication. Use your programming expertise to create a program using user defined function named login that accepts userid and password as parameters (login(uid,pwd)) that displays a message “account blocked” in case of three wrong attempts. The login is successful if the user enters user ID as "ADMIN" and password as "St0rE@1". On successful login, display a message “login successful”.
Wrap the credential check in a user-defined function login(uid, pwd) that accepts the ID and password as parameters, drive it from a 3-attempt loop, print "login successful" on the correct pair (ADMIN / St0rE@1) and "account blocked" after the third failure.
The idea. Separating policy from mechanics is good function design: the function login() holds the one rule (which credentials are valid), while the main program handles the interaction loop and the attempt counting. The loop-else of while is a neat fit — it runs only when the loop ends without break, i.e. only when all three attempts failed.
def login(uid, pwd):
# Return True if the user ID and password are correct
return uid == "ADMIN" and pwd == "St0rE@1"
attempts = 0
while attempts < 3:
uid = input("Enter user ID: ")
pwd = input("Enter password: ")
if login(uid, pwd):
print("login successful")
break
attempts += 1
print("Wrong user ID or password. Attempts used:", attempts)
else:
print("account blocked") # runs only if the loop ended without break
Sample run 1 — success on the second attempt
Enter user ID: admin
Enter password: St0rE@1
Wrong user ID or password. Attempts used: 1
Enter user ID: ADMIN
Enter password: St0rE@1
login successful
Sample run 2 — three failures
Enter user ID: admin
Enter password: abc
Wrong user ID or password. Attempts used: 1
Enter user ID: ADMIN
Enter password: store1
Wrong user ID or password. Attempts used: 2
Enter user ID: Admin
Enter password: St0rE@1
Wrong user ID or password. Attempts used: 3
account blocked
How it works
login(uid, pwd)matches the required signature and returns a Boolean; the comparison is exact, soadminorAdminfails (strings are case-sensitive) — see sample run 2's third attempt.- On success
breakskips theelse, so "account blocked" is not printed. - After three failed passes,
attempts < 3becomes False and thewhile'selseprints "account blocked".
Both the user ID and the password comparisons are case- and character-exact — St0rE@1 contains a zero, not the letter O. Real systems additionally never store passwords in plain text; at this level the exercise is about function parameters and loop control.
The program grants "login successful" only when login() is called with user ID ADMIN and password St0rE@1; any three consecutive wrong attempts exhaust the loop and it prints "account blocked".
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.