简体   繁体   English

我似乎陷入了无法脱身的困境

[英]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! ")

I want the code to ask for a color in the rainbow and if the input is not a color then ask again but if it is then print我希望代码要求彩虹中的颜色,如果输入不是颜色,则再次询问,但如果是,则打印

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.

相关问题 我似乎无法摆脱这个程序在 python 上的循环 - I can't seem to get out of the loop on this program on python 无法似乎弄清楚我写了什么排序算法 - Cant seem to figure out what Sorting Algorithm I have written 我似乎无法让我的 .append 循环这是我到目前为止所拥有的。 我的 while 功能也是如此 - i cant seem to get my .append to loop this is what i have so far. I the while function as well 我似乎无法准确地得到它,我正在得到阶乘,但似乎无法找到一种方法来放入组合公式 - i can not seem to get it exactly, i am getting the factorials but cant seem to find a way to put in the combination formula 我似乎无法摆脱的 tkinter 程序错误 - a tkinter program error i cant seem to get rid of 基本无法得到我想要的时期 - Basic cant get a period where i want it 我在 python 中制作了一个随机动漫选择器,我似乎无法找到错误所在。有人可以帮我解决这个项目吗 - I was making a random anime selector in python and I can't seem to get where the error is.Could somebody help me out with this project 我无法让 if 循环在 tkinter 代码中工作 - I cant get an if loop to work in a tkinter code 我正在制作一个 mp3 播放器,我需要一个前进按钮,但似乎无法弄清楚 - I am making a mp3 player , and i need a forward button to it but cant seem to figure it out 我似乎无法在输入框中输入 - I cant seem to be able to type in to the input box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM