Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How can you make it so while statements so for 1 variable, there can be multiple possible answers

How can you make it so while statements so for 1 variable, there can be multiple possible answers so the while statement will not run if a user inputs an answer?

For example, if you have a script that says:

favorite_color = input("What is your favorite color? ")

while favorite_color != 'green': favorite_color = input("Hmmm, thats not a color, please try again! ")

If your user inputs green, the while statement wouldn't run, but if you enter blue, the while statement would run. How do you make it so you can put as many colors/answers as you want, and not just green?

1 Answer

You just need any statement that evaluates to a boolean:

while favorite_color not in ['green','blue','red','orange','black']:
    favorite_color = input("Hmmm, thats not a color, please try again! ")