如何修复 Python 中的无限循环?
How to fix infinite loop in Python?
我在Python做一个登录安全程序,这部分总是陷入死循环。如果您被锁定,它会一直说 'you have been locked out for 10 seconds' 并无限循环。我该如何解决这个问题?
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
简单,改变
while tries>=4:
至
if tries >= 4:
如果您遵循您的代码,当您进入第二个 while 循环时,变量 tries 的值永远不会改变,因此您将永远留在其中。如果将 while 更改为 if,将执行 if 语句的主体,然后继续循环。
您的第二个 while
循环在 tries
大于四时激活,但循环本身从不在 tries
上运行或包含任何其他退出条件。这就是为什么它变得无限。所以 tries
保持在四以上并且循环永远不会停止。
从你的代码来看,在我看来,在他们用完尝试后,他们会被锁定 10 秒,然后他们会重新开始?如果是这样,您希望像这样调整您的代码:
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
我认为你应该在睡眠后将尝试设置为 0,如果不需要则循环。睡眠后用户应该再次尝试 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
我在Python做一个登录安全程序,这部分总是陷入死循环。如果您被锁定,它会一直说 'you have been locked out for 10 seconds' 并无限循环。我该如何解决这个问题?
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
简单,改变
while tries>=4:
至
if tries >= 4:
如果您遵循您的代码,当您进入第二个 while 循环时,变量 tries 的值永远不会改变,因此您将永远留在其中。如果将 while 更改为 if,将执行 if 语句的主体,然后继续循环。
您的第二个 while
循环在 tries
大于四时激活,但循环本身从不在 tries
上运行或包含任何其他退出条件。这就是为什么它变得无限。所以 tries
保持在四以上并且循环永远不会停止。
从你的代码来看,在我看来,在他们用完尝试后,他们会被锁定 10 秒,然后他们会重新开始?如果是这样,您希望像这样调整您的代码:
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
我认为你应该在睡眠后将尝试设置为 0,如果不需要则循环。睡眠后用户应该再次尝试 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