Python Forum

Full Version: Looking for a way to loop until user enters from a list?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys,

I was wondering if someone could help me out....I'm looking to write a simple code where the user is asked to enter a colour, if they enter green, blue or red it stores their answer and then moves on.... however I also want it to loop so that it will keep asking the same question until the user has entered one of those three colours...

Is there anyone that could please help me out with this? I would be so grateful!!

So far I have....
colour =  " "
colour = input ("Enter a colour from green, blue or yellow")
while colour != "blue" or "yellow" or "green"
print ("Enter a colour from the list")
The code above seemed to work when I only had one option for them to choose from, as soon as I use the 'or' it just loops no matter what input I give...

Could any one help with this please, even if the code you give is completely different to this!
This is how I would go about it:

colour =  "BashBedlam was here."
while colour not in ('blue', 'yellow', 'green') :
	colour = input ("Enter a colour from green, blue or yellow : ")
	colour = colour.lower ()
You should try searching the forum. Here's a similar question asked two days ago.

https://python-forum.io/Thread-Problem-r...ssors-game
(Mar-20-2021, 01:10 PM)BashBedlam Wrote: [ -> ]This is how I would go about it:

colour =  "BashBedlam was here."
while colour not in ('blue', 'yellow', 'green') :
	colour = input ("Enter a colour from green, blue or yellow : ")
	colour = colour.lower ()

Amazing, thank you!!!
(Mar-20-2021, 12:11 PM)PythonW19 Wrote: [ -> ]Hey guys,

I was wondering if someone could help me out....I'm looking to write a simple code where the user is asked to enter a colour, if they enter green, blue or red it stores their answer and then moves on.... however I also want it to loop so that it will keep asking the same question until the user has entered one of those three colours...

Is there anyone that could please help me out with this? I would be so grateful!!

So far I have....
colour =  " "
colour = input ("Enter a colour from green, blue or yellow")
while colour != "blue" or "yellow" or "green"
print ("Enter a colour from the list")
The code above seemed to work when I only had one option for them to choose from, as soon as I use the 'or' it just loops no matter what input I give...

Could any one help with this please, even if the code you give is completely different to this!
(Mar-20-2021, 03:15 PM)deanhystad Wrote: [ -> ]You should try searching the forum. Here's a similar question asked two days ago.

https://python-forum.io/Thread-Problem-r...ssors-game

Will bear in mind, I'm new to the forum and was looking for some quick help whilst I was working on it!
(Mar-20-2021, 12:37 PM)buran Wrote: [ -> ]https://python-forum.io/Thread-Multiple-...or-keyword


Thank you!! Smile