python - Why does this programme not stop after three password guesses? -
username = "patrick" password = "kearney" username = input("username:") while username != "patrick": print("please try again.") username = input("enter username again.") password = input("password:") incorrect = 0 threshold = 3 while password != "kearney": print("please try again.") password = input("enter password again.") if password != "kearney": incorrect += 1 else: print("welcome patrick") incorrect = 0 if incorrect == threshold: import sys sys.exit()
for if executed inside while loop, should indent properly, should work:
username = "patrick" password = "kearney" username = input("username:") while username != "patrick": print("please try again.") username = input("enter username again.") password = input("password:") incorrect = 0 threshold = 3 while password != "kearney": print("please try again.") password = input("enter password again.") if password != "kearney": incorrect += 1 else: print("welcome patrick") incorrect = 0 if incorrect == threshold: import sys sys.exit() edit: @benjamin pointed out in comments going allow 4 trials (1 outside while loop , 3 inside loop), set threshold 3 or incorrect 1 @ beginning fix this.
Comments
Post a Comment