[英]I seem to be in a predicament where I cant get out of the loop
rainbow_list = ["Red", "Orange", "Yellow", "Green", "Blue", "indigo", "violet"]
color_tires = 0
color = input("enter a color of the rainbow: ")
while color_tires <= 4:
if color != rainbow_list:
color = input("enter a color of the rainbow: ")
color_tries =+ 1
else:
print("That is a correct color! ")
There were a few things working against you in your code preventing it from identifying a correct answer and exiting the "while" loop.在您的代码中有一些对您不利的事情,阻止它识别正确的答案并退出“while”循环。 First off, you had some typos in your spelling of "color_tries" (eg "color_tires").
首先,您在“color_tries”的拼写中有一些拼写错误(例如“color_tires”)。 So that was throwing you off.
所以这让你失望了。 Next, the variable "color_tries" within the "if" block was being treated as a local variable, and so was actually getting reset every time.
接下来,“if”块中的变量“color_tries”被视为局部变量,因此实际上每次都被重置。 Thus the value comparison in the "while" loop was never reached and you stayed stuck in that loop.
因此,从未达到“while”循环中的值比较,并且您一直停留在该循环中。 Also, the "if" test was also always going to be false as it was attempting to compare an entered text color value against a list of colors.
此外,“if”测试也总是错误的,因为它试图将输入的文本颜色值与颜色列表进行比较。 What you would want to do is check if the entered color was in (or not in) the color list.
您想要做的是检查输入的颜色是否在(或不在)颜色列表中。 With that, I made some tweaks to your program but still keep the spirit of the game.
有了这个,我对你的程序做了一些调整,但仍然保持了游戏的精神。
rainbow_list = ["Red", "red", "Orange", "orange", "Yellow", "yellow", "Green", "green", "Blue", "blue", "Indigo", "indigo", "Violet", "violet"]
color = input("Enter a color of the rainbow: ")
color_tries = 0
while (1):
color_tries += 1
print('Tries: ', color_tries)
if color_tries > 4:
break
if color not in rainbow_list: # rainbow_list is a list so equality testing won't work - us "in" or "not in" testing
color = input("Enter a color of the rainbow: ")
else:
print("That is a correct color! ")
break
Just to make it a little friendlier, I expanded the color list in case a user were to enter in the color value completely in lower case letters.为了让它更友好一点,我扩展了颜色列表,以防用户完全用小写字母输入颜色值。 Then, I refined the "while" loop so that it technically never ends until a "break" statement is reached.
然后,我改进了“while”循环,使其在技术上永远不会结束,直到达到“break”语句。 I also moved the increment of the tries above the "if" statement so that it was affecting the global variable known as "color_tries".
我还将尝试的增量移到“if”语句上方,以便它影响称为“color_tries”的全局变量。 Then, so as to make the "if" test work, I check if the entered color is not in the color list, and if so increment the color_tries counter.
然后,为了使“if”测试工作,我检查输入的颜色是否不在颜色列表中,如果是,则增加 color_tries 计数器。 If and when the limit of tries is reached or a correct color is guessed, the program will the break out of the "while" loop and finish.
如果达到尝试的限制或猜到正确的颜色,程序将跳出“while”循环并完成。
Please review the tweaked code.请查看调整后的代码。 I hope that clarifies things for you as you move forward creating programs and games.
我希望在您继续创建程序和游戏时为您澄清事情。
Regards.问候。
#i wish i help you
rainbow_list = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo",
"Violet"]
color_tries = 0
while_loop = 0
while color_tries <= 4 and while_loop == 0:
color = input("enter a color of the rainbow: ")
for i in rainbow_list:
if i == color :
print("That is a correct color! ")
while_loop += 1
color_tries += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.