简体   繁体   English

如何修复Python中的无限循环?

[英]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. 我正在用Python开发一个登录安全程序,而这部分总是陷入无限循环中。 If you get locked out, it keeps saying that 'you have been locked out for 10 seconds' and it loops infinitely. 如果您被锁定,它会一直说“您已被锁定10秒钟”,并且无限循环。 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. 如果你按照你的代码,当你进入你的第二个while循环,变量的尝试值不会改变,所以你会留在了那里所有的永恒。 If you change the while to an if, you will execute the body of the if statement then continue your loop. 如果将while更改为if,则将执行if语句的主体,然后继续循环。

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. tries大于4时,您的第二个while循环会激活,但是循环本身永远不会对tries或包含任何其他退出条件。 That is why it becomes infinite. 这就是为什么它变得无限大的原因。 So tries remains above four and the loop never ceases. 因此tries保持在4以上,循环永远不会停止。

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? 从您的代码看来,在他们用尽尝试之后,他们被锁定了10秒钟,然后又可以重新开始了? 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. 我认为您应该在睡眠后将try设置为0,并在不需要时进行循环。 After sleep user should get another 5 tries again. 睡眠后,用户应再次尝试5次。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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