简体   繁体   中英

How to fix infinite loop in Python?

I am making a login security program in Python, and this part always gets stuck in in infinite loop. If you get locked out, it keeps saying that 'you have been locked out for 10 seconds' and it loops infinitely. How do I fix this?

tries=0
while finalusername!=username or  finalpassword!=password:
tries=tries+1
print "That incorrect. Try again."
print "You have", 5-tries, "tries left."
finalusername= raw_input ("Username:")
finalpassword= raw_input ("Password:") 
while tries>=4:
    print "You have been locked out for 10 seconds. Please call the administrator if this keeps happening."
    sleep (10.0)
    system ('CLS')
    finalusername!=username
    finalpassword!=password

Easy, change

  while tries>=4:

to

  if tries >= 4:

If you follow your code, when you get into your second while loop, the value of the variable tries never changes, so you will stay in there for all of eternity. If you change the while to an if, you will execute the body of the if statement then continue your loop.

Your second while loop activates when tries is greater than four, but the loop itself never operates on tries or contains any other exit condition. That is why it becomes infinite. So tries remains above four and the loop never ceases.

From your code, it seems to me that after they use up their tries, they get locked out for 10 seconds and then they get to start again? If so you would wish to adjust your code like this:

tries=0
while finalusername!=username or  finalpassword!=password:
    tries=tries+1
    print "That incorrect. Try again."
    print "You have", 5-tries, "tries left."
    finalusername= raw_input ("Username:")
    finalpassword= raw_input ("Password:") 
    if tries>=4:
        print "You have been locked out for 10 seconds. Please call the administrator if this keeps happening."
        sleep (10.0)
        system ('CLS')
        tries = 0 #Reset tries counter
        finalusername!=username #I don't think this part is required
        finalpassword!=password #I don't think this part is required

I think you should set tries to 0 after sleep, and the loop if not necessary. After sleep user should get another 5 tries again.

import os
import time

tries=0
finalusername, finalpassword= 'a', 'a'
username, password = None, None


while True:
    finalusername= raw_input ("Username:")
    finalpassword= raw_input ("Password:") 
    if finalpassword == username and finalpassword == password:
        break
    tries=tries+1
    print "That incorrect. Try again."
    print "You have", 5-tries, "tries left."
    if tries>=5:
        print "You have been locked out for 10 seconds. Please call the administrator if this keeps happening."
        time.sleep (10.0)
        os.system('cls')
        tries = 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM