Python Forum
Infinite loop/ only half working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Infinite loop/ only half working
#1
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:

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 
Reply
#2
The question for the user needs to be in the loop, otherwise it can only be asked once.

There is another minor issue, note that
>>> rainbow = "red orange yellow green blue indigo violet"
>>> "ange yel" in rainbow
True
Reply
#3
Hi, thank you for this! I moved the question into the loop, and added commas for separation. I believe my problem had to do with the counter location, but I think I figured it out. Thank you!
Reply
#4
(Sep-08-2018, 10:52 PM)anclark686 Wrote: added commas for separation
I think Griboullis suggest to make it a list/tuple/set
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
also the break after the first if statement needs not be there, because if True, then the loop runs, with break it stops
Reply
#6
(Sep-09-2018, 07:19 AM)gitiya Wrote: also the break after the first if statement needs not be there, because if True, then the loop runs, with break it stops
that is the idea - break out of the loop if color in rainbow...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Infinite loop Danado 4 2,351 Aug-16-2021, 05:56 PM
Last Post: deanhystad
  Help with while loop creating an infinite loop. FWendeburg 3 3,027 Jan-30-2019, 08:28 PM
Last Post: FWendeburg
  Why is this giving me an infinite loop? wlsa 4 3,893 Jul-25-2018, 10:11 PM
Last Post: cyberpatje
  Another infinite loop wlsa 7 4,643 Jul-20-2018, 12:04 AM
Last Post: wlsa
  How to stop an infinite loop in spyder if it's currently running? wlsa 3 24,682 Jun-30-2018, 03:27 AM
Last Post: ichabod801
  For and If Loop not working as intended Jaykin 1 2,589 Apr-29-2018, 01:14 PM
Last Post: j.crater
  Infinite loop Truman 9 9,073 Jan-19-2018, 11:25 PM
Last Post: j.crater
  While loop/half-life Miraclefruit 6 8,442 Mar-06-2017, 05:24 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020