Checking multiple conditions in while statements for python [duplicate] - python

This question already has answers here:
How to simplify multiple or conditions in an if statement?
(1 answer)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
while MenuChoice != '1' or '2' or '3':
MenuChoice = input('What would you like to do? (Type a number):')
This is the code I have at the moment, however, when the code is executed, the MenuChoice user input is repeated no matter what character is entered (this includes 1, 2, and 3)
How can I make it so that the program checks if the user's input for MenuChoice (prior to this section to code) was '1', '2', or '3' and if not, repeat the MenuChoice input?
Thank you in advanced.

Related

My python variables are not being defined [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
What's the canonical way to check for type in Python?
(15 answers)
How to use user input to end a program?
(6 answers)
Asking the user for input until they give a valid response
(22 answers)
Closed 22 days ago.
Here's the beginning of my program:
import sys
print("Enter a number when prompted, type \"stop\" to stop.")
x = input("Input: ")
if type(x) == "int":
Higher = x
Lower = x
elif x.lower() == "stop":
print("The program has been stopped.")
sys.exit()
If right after this I try to print the variable Higher, for example, I get this error message: NameError: name 'Higher' is not defined
EDIT: I tried declaring the variables beforehand but that doesn't work either.

If statement calling both functions [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 9 months ago.
I am trying to call a python function inside of an if when you input a certain letter. Even if the letter is not meant to be noticed by the if statement it calls both functions.
if printWrite == 'p' or 'P':
printNum()
if printWrite == 'w' or 'W':
writeNum()
Always calls both functions.

Python: Program not checking full or statement [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
some assistance is needed:
I've been having some trouble with the OR operand ...I understand how it works ( either or condition must be true), but i'm not sure why it is behaving in the fashion explained below.
The 'or' statement doesn't seem to be checking the second condition in my "flowContol' function.. when I enter 'y' it sees the condition as true and runs without any problems... however when i enter 'yes' it evaluates the condition as false...
-I excluded the other functions-..
def flowControl():
answer = input("do you want run the 'displayLession' function? ( yes or
no)").strip()
if answer == ('y' or 'yes'):
displayLesson()
else:
userTime()
print('End program')
flowControl()
What you are looking for is
if answer == 'y' or answer == 'yes':
Or
if answer in ['y', 'yes']:
Why? Simple, your code evaluates before what is between parentheses 'y' or 'yes' and this is 'y', because 'y' is a truthy expression in python, so no need to evaluate the latter expression.

Python: Shorter way of checking if a string equals two other strings [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
have just started learning python and this question came up to my mind
is there a shorter way to determine if a string equals 'something' or 'somethingelse'?
example:
input = raw_input("question?")
while input != 'n' and input != 'y':
#ask question again
You could check whether it is in a list or set.
input = raw_input("question?")
while input not in ['n', 'N']:
#ask question again
If you are just trying to accept two cases though, you could also just call lower on the input.
while input.lower() != 'n':
#ask question again
Perhaps unexpectedly, 'a' != input != 'b' works. It resolves the same way as ('a' != input) and ('b' != input). You can do the same thing with ==, or with <, >, etc. on numbers, as well.
Oh, but you have to be careful if you chain it longer than three things, or use multiple different comparison operators.
If it's a case issue, then you can:
while input.lower() != 'n'

Why is is-operator not functioning like ==? [duplicate]

This question already has answers here:
Why does comparing strings using either '==' or 'is' sometimes produce a different result?
(15 answers)
Closed 9 years ago.
If I understand correctly, the is operator can take the place of ==.
Why when I write
if inpty == "exit":
return
does the function exit, but when I write
if inpty is "exit":
return
the function does not?
inpty is the value of the input.
is compares identity, whereas == compares equality.
In other words, a is b is the same as id(a) == id(b).
because in this case, the is operator is testing identity, not the value.

Categories