Sep-08-2018, 10:19 PM
Hi all - I'm auditing a MOOC, and have to create code for the following question:
# [ ] use a while True loop (forever loop) to give 4 chances for input of a correct color in a rainbow
# rainbow = "red orange yellow green blue indigo violet"
When I enter a color that isn't in "rainbow" it causes an infinite loop. If I enter one that is in "rainbow" it performs as it should. Any help is greatly appreciated!
Here's my code:
# [ ] use a while True loop (forever loop) to give 4 chances for input of a correct color in a rainbow
# rainbow = "red orange yellow green blue indigo violet"
When I enter a color that isn't in "rainbow" it causes an infinite loop. If I enter one that is in "rainbow" it performs as it should. Any help is greatly appreciated!
Here's my code:
rainbow = "red orange yellow green blue indigo violet" color = input("Guess a color of the rainbow: ") counter = 0 while True: if color.lower() in rainbow: print("Correct!", color.capitalize(), "is a color in the rainbow!") break elif counter < 4: if color.lower() in rainbow == False: counter += 1 print("Sorry that color is not in the rainbow.") else: break