0

How do I get it so that I can make a while loop work with 'or' for example

    start = input(("Would you like to start? "))
    while start == "yes" or "YES" or "Yes":

Then later on I have the code

    start = input(("Would you like to start again? "))
    if start == "no" or "No" or "NO":
       break

When I try this code it does not work. It starts the code at the beginning and breaks at the end no matter what I enter. Can anyone help?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497

3 Answers3

2

Since or has higher precedence than ==,

start == "yes" or "YES" or "Yes":

will be evaluated as

(start == "yes") or ("YES") or ("Yes")

You can simply do

while start.lower() == "yes":

The same way,

if start.lower() == "no":
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

each statement between the or is separated. so you actually check if start == "yes" is True or "YES" is True etc.
since "YES" is not an empty string, it considered as True boolean value.

I want to change it to something like:

while (start == "yes") or (start == "YES") or (start == "Yes"):

or even:

while start.lower() == "yes":
Elisha
  • 4,811
  • 4
  • 30
  • 46
0

Instead of this:

while start == "yes" or "YES" or "Yes":

do this (same with the if one as well):

while start == "yes" or start== "YES" or start == "Yes":

or, even better, do this:

while start.lower() == "yes":

You could also do:

while start.lower().startswith('y'):

so if the user enters anything starting with 'y', it will do whatever is in the while statement.

ᔕᖺᘎᕊ
  • 2,971
  • 3
  • 23
  • 38