Python Forum
Looking for a way to loop until user enters from a list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looking for a way to loop until user enters from a list?
#1
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!
buran write Mar-20-2021, 12:36 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
https://python-forum.io/Thread-Multiple-...or-keyword
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
#3
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 ()
Reply
#4
You should try searching the forum. Here's a similar question asked two days ago.

https://python-forum.io/Thread-Problem-r...ssors-game
Reply
#5
(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!!!
Reply
#6
(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!
Reply
#7
(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!
Reply
#8
(Mar-20-2021, 12:37 PM)buran Wrote: https://python-forum.io/Thread-Multiple-...or-keyword


Thank you!! Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  User input/picking from a list AnunnakiKungFu 2 542 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
Question Reset list if user regrets Gilush 1 706 Dec-05-2020, 10:55 AM
Last Post: Gilush
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 907 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  Appending to list of list in For loop nico_mnbl 2 579 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Append list into list within a for loop rama27 2 786 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  Nested Loop for user input Ads 2 1,551 Dec-30-2019, 11:44 AM
Last Post: Ads
  Help with calling list from user input farispython 5 1,082 Nov-03-2019, 03:13 PM
Last Post: Gribouillis
  Creating csv header from user-input list dvanommen 2 1,049 Aug-26-2019, 08:51 PM
Last Post: dvanommen
  Server infinite loop input from user tomislav91 1 1,677 May-23-2019, 02:18 PM
Last Post: heiner55
  User input - list Ondra 3 1,266 Feb-15-2019, 11:29 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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