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 8 years ago.
I am working on a project that at one point will ask the user a yes/no question. I currently use this code to handle such questions:
def yn():
global finalchoice
choice=str(raw_input("Y/N: "))
if choice == "Y":
finalchoice="true"
elif choice == "y":
finalchoice="true"
elif choice == "N":
finalchoice="false"
elif choice == "n":
finalchoice="false"
else:
yn()
pass
but this seems to be quite inefficient, specifically where I have to check for both "Y" and "y" or "N" and "n" separately. I've tried:
if choice == "Y" or "y":
finalchoice="true"
Unfortunately, all this does is ignore the 'else' command and will pass whatever I give it.
Any tips?
if choice == "Y" or "y": is not right! It will always evaluate to True.
It essentially is (choice == "Y") or ("y"). And the latter is True because any non empty string in python logically evaluates to boolean True.
You should be doing:
if choice in ["Y","y"]:
Or,
if choice == "Y" or choice == "y":
Or you can use:
if choice.lower() == "y":
finalchoice="true"
To add to #Sudipta's answer, one way would be to remove the case AND just take the first letter:
if choice.lower().startswith("y"):
finalchoice="true"
Related
This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 14 days ago.
I am just starting to learn to code and I am experimenting with all sorts of methods. Might be a dumb question but why does this block only return the 0 value regardless of what I input?
start = input("Would you like to play? (Y/N)\n")
if start == "N" or "n":
game_state = 0
elif start == "Y" or "y":
game_state = 1
I have tried to change it in all sort of ways but I can t seem to make it work.
You need to change your if statements to:
if start == "N" or start == "n":
or to:
if start.lower() == "n":
A non-empty string will evaluate to True, so the first if statement will always run.
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 4 months ago.
I don't know how exactly to word it so ill just explain,
elif careful != "yes" "no":
How do i make the "yes" and "no" independent? When i put it in, it combines the "yes" and "no" together.
Output 👇
'yesno'
I have tried using the '|' operator and the '&' operator
Here is the full code 👇
careful = input("Are you Careful?")
if careful == "yes":
print("ok good, what is your age?")
age = int(input("Your age?"))
if age <= 13:
print("sorry, paws is hiding")
elif careful != "yes" "no":
print("nonsense, say yes or no")
else:
print("Be careful! she nibbles, with no teeth")
else:
print("you have to be careful!")
thanks
Instead of,
elif careful != "yes" "no":
You can use
elif careful not in ("yes" , "no"):
This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 10 months ago.
im trying to do url shortener tool , but im facing a problem that when the link is done
and the tool will ask the user if he want to do it again
so the problem here that if user inputed n the tool won't exit
Code :
again = input(f"{r}[?]{b}Do you want to short another link (y/n) ==> ")
if again == 'y'or'Y':
os.system('cls')
logo()
link = input(f"{r}[/]{b}Enter the link (google.com) --> ")
elif again == 'n'or'N':
print(f"{r}[-]{b}Thanks for using the tool !")
exit()
else:
print(f"{r}[!]{b}Not a valid option")
what is the error here please help me
You're not evaluating what you think you're evaluating.
if again == 'y' or 'Y':
is not the same as
if again == 'y' or again == 'Y':
in your terminal, try
if "Y":
print(True)
you'll see that it evaluates to True.
So you're always going to enter that first if statement.
Try
if again == "y" or again == "Y":
or
if again.lower() == "y":
or
if again in ["y", "Y"]:
and the same changes for the elif
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 3 years ago.
When I say "n" or "no" for the input if works fine but when i say "y" or "yes" for the input it just does the same thing as for "no". everything else in the program runs perfectly apart from this. i have absolutely no clue as to why this is happening.
def restart():
replay = input("Do you want to restart? (Y/N): ")
if replay.lower() == "n" or "no":
print("exiting")
sys.exit()
if replay.lower() == "y" or "yes":
calc()
else:
print("Unknown Command")
restart()
restart()
The conditions in your if statements aren't evaluating the way you think they are. When you use a logical operator like the or you have, it evaluates first the part before the or, and then the part after the or, and if either is true the whole statement is true.
So instead of
if replay.lower() == "n" or "no": #always runs because just "no" evaluates as being true
use
if replay.lower() == "n" or replay.lower == "no":
and make a similar change to your if statement which tests for yes.
Replace this:
if replay.lower() == "n" or "no":
print("exiting")
sys.exit()
if replay.lower() == "y" or "yes":
calc()
With this:
if replay.lower() == "n" or replay.lower() == "no":
print("exiting")
sys.exit()
if replay.lower() == "y" or replay.lower() == "yes":
calc()
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
Hello programmers of StackOverFlow, I am a teenager that studies Python, And I can't find the logical errors in one of my codes. I wish you could help with that.
I got two logical errors.
1. If the answer is "yes" or "no" it keeps opening the while commands while it shouldn't.
2. Concluding from first error it never stops when the choice == "yes" or "no"..
My coding is here:
choice = raw_input("Do you like programming? (yes/no) : ")
while choice != "yes" or "no":
choice = raw_input('Come on, answer with a "yes" or with a "no" ! : ')
if choice == "yes":
print "I like you !"
elif choice == "no":
print "I am sorry to hear that .. "
, Thank you in advance !
the second line evaluates to True. Because the string "no" evaluates to true
try the following
if "any string": print(1) #will always print 1, unless the string is empty
What you should use instead is
while choice not in ["yes", "no"]:
which checks if choice matches either "yes" or "no"
This is the problem:
while choice != "yes" or "no":
This is interpreted by Python as (choice != "yes") or ("no"). Since "no" is a string of non-zero length, it is truthy. This makes the or expression true because anything OR true is true. So your condition is always true and the loop never stops.
It should be:
while choice != "yes" and choice != "no":
Or:
while choice not in ("yes", "no"):