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
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 990 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  user input values into list of lists tauros73 3 1,023 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  functional LEDs in an array or list? // RPi user Doczu 5 1,521 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  Multiple user defined plots with secondary axes using for loop maltp 1 1,393 Apr-30-2022, 10:19 AM
Last Post: maltp
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  User input/picking from a list AnunnakiKungFu 2 2,282 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
Question Reset list if user regrets Gilush 1 2,043 Dec-05-2020, 10:55 AM
Last Post: Gilush
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 2,883 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  Appending to list of list in For loop nico_mnbl 2 2,318 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Append list into list within a for loop rama27 2 2,307 Jul-21-2020, 04:49 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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