#πŸ”’ Input validation while loop help

65 messages Β· Page 1 of 1 (latest)

lean slate
#

Hello taking intro to python and struggling with validating inputs. I need this while loop to repeat if the input is not equal to Y y or N n, but its not working. I can only use stuff we've learned so far so I don't think I can use while true or functions. Please help, thanks

while repeat != 'Y' or 'y' or 'n' or 'n':

    print('Error - please enter y/n')
    
    repeat = input('Would you like to enter another (y/n) ?')

trying to loop an entire program while repeat is == 'y' or 'Y'

tight flareBOT
#

@lean slate

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mint patio
#

!or

tight flareBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
mint patio
#

Does that make sense?

lean slate
#

yea, but would i put the while loop in the if statement

mint patio
#

you can replace if with while

#

both while and if are keywords in python that evaluate an expression and do a thing if the expression is truthy

lean slate
#

ok so i got it so my whole program shoud loop

mint patio
#

that is up to you

lean slate
#

but i am also trying to write it so it repeats asking for input

#

oh i think i figured it out thanks

#

while repeat != 'Y' or repeat !='y' or repeat != 'n' or repeat != 'N':

        print('Error - please enter y/n')
        
        repeat = input('Would you like to enter another (y/n) ?')
tight flareBOT
#

Hey @lean slate!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
lean slate
#

py while repeat != 'Y' or repeat !='y' or repeat != 'n' or repeat != 'N':

        print('Error - please enter y/n')
        
        repeat = input('Would you like to enter another (y/n) ?')
#
        
            print('Error - please enter y/n')
            
            repeat = input('Would you like to enter another (y/n) ?')```
#

what am i doing wrong, no matter what input i put it runs the while loop

#

oh the input statment is repeat = input('Would you like to enter another (y/n) ?')

mint patio
#

Did you figure it out?

lean slate
#

no

#

i am trying to watch video

#

but its not helping

#

does it not work with the != operator

mint patio
#

Does what not work with the != operator?

lean slate
#

well i was asking about the while loop but i dont see any reason it wouldnt

#

i want to use a while loop to evaluate whether a variable is equal to 4 different strings and if not i want the prompt for my input to repeat

#

should i post all the code

#

so you u can see what i am trying to do

mint patio
#

sure

#

!paste

tight flareBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

lean slate
mint patio
#

Ok, and the problem is that commented loop towards the end?

#

What happens when you enter 'y'?

lean slate
#

not matter what i enter, the while loop runs and prints the error message

#

yea

mint patio
#

Are you sure you're saving the file after you change it?

lean slate
#

yes

#

it turns into infinite loop cause exit statement doesnt get fufilled

#

wouldnt that be checking if its true or false

mint patio
#

My preferred way to check multiple things like this is that second example

#

!or

tight flareBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
mint patio
#

@lean slate while repeat != 'Y' or repeat !='y' or repeat != 'n' or repeat != 'N' <- step through this logically.

#

what happens if you enter 'y'?

lean slate
#

it returns not equal

mint patio
#

ok, now what happens if you enter 'Y'?

lean slate
#

same

mint patio
#

and we're not returning, we're not in a function

#

so do you understand why it keeps looping?

#

anything you enter will not satisfy your expression

#

so it keeps looping

lean slate
#

yea

#

so im thining if repeat is == to those values than have it do nothing

mint patio
#

!e ```py
s = "hello"
if 'l' in s:
print("found l!")

tight flareBOT
#

@mint patio :white_check_mark: Your 3.12 eval job has completed with return code 0.

found l!
mint patio
#

conversely

#

!e ```py
s = "hello"
if 'x' not in s:
print("no x here!")

tight flareBOT
#

@mint patio :white_check_mark: Your 3.12 eval job has completed with return code 0.

no x here!
mint patio
#

in and not in are both very useful

#

they work with any Python built-in types

lean slate
#

thanks for helping i got it

tight flareBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

πŸ”’ Input validation while loop help