Python Forum
How do i add limits to this code ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do i add limits to this code ?
#1
while True:
  answer1 = input("Password: ")
  if answer1 != "123":
    print("Wrong Password")
    continue
  else:
    print("Welcome!")
    break
Hi , i would like to ask , if i would like to add a limit to how many times a user can input password, how
should i do it ?

Many thanks in advance
Reply
#2
set a counter before entering loop
within loop:
increment counter on each iteration
break (with message) after limit reached.
Reply
#3
The easiest way I know is to add a count to each time through the loop:
attempts = 0

while True:
    attempts += 1
    if attempts < 5:
        
       #check password 
           
    else:
        break
Reply
#4
Another option is a for loop instead of a while loop.

for _ in range(5): # Will loop exactly 5 times
  answer1 = input("Password: ")
  if answer1 != "123":
    print("Wrong Password")
    continue
  else:
    print("Welcome!")
    break
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Thread Limits . . . JohnnyCoffee 10 1,571 Mar-03-2023, 04:07 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020