![](/img/trans.png)
[英]How to send websocket updates to any connected clients while running a while-True loop?
[英]How do I exit a while-true loop after 5 tries?
在我对计算机科学课程的介绍中,我们遇到了一个问题,我们必须创建一个循环来要求输入人员密码:
while True:
password = input('What is your password?')
if password == "abc123":
break
print("Please Try Again")
print("Welcome!")
我如何更改它,以便在 5 次尝试/猜测密码后,它显示“密码猜测全部结束”(或类似的东西)?
很多人不熟悉for...else
结构,在这种情况下这是经典的
for attempt in range(5):
password = input('What is your password?')
if password == "abc123":
print("Welcome!")
break
else:
print("all out of password guesses")
只有在没有遇到break
才会执行else
我同意@mauve 的观点,即while
循环并不是您要查找的内容,但您仍然可以使用计数器来实现:
max_tries = 5
while max_tries > 0: # We will decrement max_tries on each loop
password = input('What is your password?')
if password == "abc123":
break
print("Please Try Again")
max_tries -= 1 # Decrement max_tries
if max_tries==0: # We tried too many times
raise ValueError("Too many attempts!")
但是,使用 for 循环可能会更清楚一些
for i in range(max_tries):
password = input('What is your password?')
if password == "abc123":
break
print("Please Try Again")
if i == max_tries:
raise ValueError("Too many attempts")
您可以在 for 循环的末尾使用else
,如下所示:
for i in range(max_tries):
password = input('What is your password?')
if password == "abc123":
break
print("Please Try Again")
else:
raise ValueError("Too many attempts")
else
将捕获在循环结束之前没有调用break
的情况
实际上,如果您有循环限制,它就不是真正的“while true”。 您可以通过简单地检查密码 5 次(或 n 次)来实现相同的目的。
try_num = 0
while try_num <= 5:
try_num = try_num + 1
<rest of the code>
如果对于评估者/教师/作业所期望的特定格式,您必须有一个 while True ,您仍然可以使用此计数器并在while True
内中断。
try_num = 0
success = False
while True:
try_num = try_num + 1
password = input('What is your password?')
if password == "abc123":
success = True
break
if try_num > 5:
break
print("Please Try Again")
if success == True:
print("Welcome!")
您可能会看到选项 1 更优雅且更易于维护。
或者,您可以使用while ... else
循环:
attempts = 0
while attempts < 5:
password = input('What is your password?')
if password == "abc123":
print("Welcome!")
break
print("Please Try Again")
attempts += 1
else:
print('You have exceeded the number of allowed login attempts!')
做一个计数器,让它倒计时。 while
循环的条件应该是“当计数器达到 0 时”:
counter = 5
while counter > 0:
counter -= 1
password = input('What is your password?')
if password == "abc123":
break
print("Please Try Again")
print("Welcome!")
与正确获取密码相比,您可能需要重写一些内容,以便在计数器超时时发生不同的事情。
或者,更正确的版本是使用for
循环而不是while
循环:
for i in range(5): # will execute 5 times with i = 0, 1, 2, 3, 4 in that order
...
但是如果您没有将i
变量用于任何特别的事情,那么一段while
也可以正常工作。
我真的不仅在 python 中而且在编程方面都是菜鸟。 只是在隔离期间学习。 我想出了这个代码来完成操作的要求。 我正在做一个在线课程和活动要求它。 这对您来说可能看起来很愚蠢,并且肯定有更好的方法来做我在这里所做的事情,但这是有效的。 (该活动要求使用 While True 进行)
rainbow = ("red, orange, yellow, green, blue, indigo, violet")
while True:
color_input = input("Enter a color of the rainbow: ")
if color_input.lower() in rainbow:
print ("Great, you did it!! ")
break
else:
print ("Wrong, you still have 3 tries.")
while True:
color_input = input("Enter a color of the rainbow: ")
if color_input.lower() in rainbow:
print ("Great, you did it")
break
else:
print ("Wrong, you stil have 2 tries")
while True:
color_input = input ("Enter a color of the rainbow: ")
if color_input.lower() in rainbow:
print ("Great, you did it")
break
else:
print ("Wrong, last try")
while True:
color_input = input("Enter a color of the rainbow: ")
if color_input.lower() in rainbow:
print ("Great, you finally did it")
break
else:
print ("You ran out attemps. Sorry, you failed")
break
break
break
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.