Home > other >  I seem to be in a predicament where I cant get out of the loop
I seem to be in a predicament where I cant get out of the loop

Time:07-16

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

CodePudding user response:

There were a few things working against you in your code preventing it from identifying a correct answer and exiting the "while" loop. First off, you had some typos in your spelling of "color_tries" (e.g. "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. Thus the value comparison in the "while" loop was never reached and you stayed stuck in that loop. 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. 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. I also moved the increment of the tries above the "if" statement so that it was affecting the global variable known as "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 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.

Please review the tweaked code. I hope that clarifies things for you as you move forward creating programs and games.

Regards.

CodePudding user response:

#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
  • Related