在我的计算机科学课程简介中,我们有一个问题,我们必须创建一个循环来要求一个人密码:
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 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")
您可以在循环的末端使用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
的情况
实际上不是真正的" true",如果您有循环限制。您可以通过简单地检查密码5(或n次)来实现同一件事。
try_num = 0
while try_num <= 5:
try_num = try_num + 1
<rest of the code>
如果您必须对评估员/老师/分配期望的特定格式有一个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!')
制作A 计数器,并将其计算。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!")
您可能需要重写一些事情才能使您与计数器呆在一起时发生不同的事情,而与正确的密码相比。
另外,A 更正确的版本是使用for
循环而不是while
循环:
for i in range(5): # will execute 5 times with i = 0, 1, 2, 3, 4 in that order
...
但是,如果您不使用i
变量,尤其是任何内容,while
也将同样工作。
我真的不仅在python中,而且在编程中。只是在上界时学习。我想出了此代码来完成OP要求的事情。我正在进行在线课程,活动要求。它看起来对您来说可能很愚蠢,并且可以确保有更好的方法可以做我在这里所做的事情,但是这有效。(在真实的情况下要求这样做的活动)
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