This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
ans = input("Enter yes or no: ")
if ans != "Yes" or "yes" or "no" or "No":
print("Can't do that")
if ans == "yes" or "Yes":
print("Great!")
if ans == "no" or "No":
print("Okay, then")
If I type let's say "Okay" it outputs this:
Can't do that!
Great!
Okay, then
instead of "Can't do that". I don't know what's wrong, and I couldn't find questions like this.
do instead something more pythonic:
if ans.lower() in ['no', 'yes']:
and use elif instead of doing another if verification.
Use in:
ans = input("Enter yes or no: ")
if ans in ["Yes", "yes"]:
print("Great!")
elif ans in ["No", "no"]:
print("Okay, then")
else:
print("Can't do that")
Test whether the answer is in a list or a set (using sets in the example below). Otherwise, your first condition evaluates to True. This is because of the operator precedence, Python considers it equivalent to (ans != "Yes") or ("yes") or ("no") or ("No"). And "yes" is True because it is not an empty string (docs), which makes the whole expression evaluate to True as well.
ans = input("Enter yes or no: ")
if ans not in {"Yes", "yes", "no", "No"}:
print("Can't do that")
if ans in {"yes", "Yes"}:
print("Great!")
if ans in {"no" or "No"}:
print("Okay, then")
Better still, make it shorter like so:
ans = input('Enter yes or no: ').lower()
if ans == 'yes':
print('Great!')
elif ans == 'no':
print('Okay, then')
else:
print("Can't do that")
That's not the way to use logical operators in a programming language, it should be:
if (ans != "Yes") or (ans != "yes") or (ans != "no") or (ans != "No")
As you see, you should always repeat your variable.
the or operator assignment is not right please try this
ans = input("Enter yes or no: ")
if ans != "Yes" or "yes" or "no" or "No":
print("Can't do that")
if ans == "yes" or "Yes":
print("Great!")
if ans == "no" or "No":
print("Okay, then")
you can either write
if ans in ('stringone', 'stringtwo'):
dosomething()
Or you can write separate equality tests,
if var == 'stringone' or var == 'stringtwo':
dosomething()
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 2 years ago.
This might be a really dumb question, but I can't see why this loop doesn't end. I'm aware that while True is an infinite loop, but I'm pretty sure there are also ways to get out of them. Does anyone know how to end this code, so that if they enter "yes" etc. it continues on through the program but if they enter "no" etc. or anything else it asks them for their name again
while True:
user_name = input("What do you want your name to be? (Suggest using Forename and Surname!")
user_name_check = input("Your name is " + user_name + "? Is this correct?")
if user_name_check == "yes" or "Yes" or "y" or "Y":
break
elif user_name_check == "no" or "No" or "n" or "N":
pass
else:
print("Sorry, please only enter yes or no. Re-enter your name and try again.")
pass
try
if user_name_check == "yes" or user_name_check == "Yes" or user_name_check == "y" or user_name_check == "Y":
This question already has answers here:
OR statement handling two != clauses Python
(5 answers)
Closed 3 years ago.
I can't seem to find the problem in code...
user_decision = ""
while not user_decision == "yes" or not user_decision == "no":
user_decision = input("You want to Join?: Please answer (yes/no): ")
else:
if user_decision == "yes":
print("test")
else:
print("test")
Thanks....
I cleaned it up a bit:
user_decision = ""
while (user_decision != "yes") and (user_decision != "no"):
user_decision = input("You want to Join?: Please answer (yes/no): ")
if user_decision == "yes":
print("test yes")
else:
print("test not yes")
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
For some reason, no matter what user_input is (Yes, No, "", eserw3) the first if statement will always be triggered. Any insight as to why the elif and the else never get activated? (The below code compiles perfectly without any errors)
Thank you in advance.
def retry():
user_input = raw_input("Would you like to face %s again? (Yes/No)" % (Enemy))
if user_input == "Yes" or "yes":
respawn()
getMove()
elif user_input == "No" or "no":
print "Thanks for playing!"
else:
print "Please enter either Yes or No."
def retry():
user_input = raw_input("Would you like to face %s again? (Yes/No)" % (Enemy))
if user_input == "Yes" or user_input == "yes":
respawn()
getMove()
elif user_input == "No" or user_input == "no":
print "Thanks for playing!"
else:
print "Please enter either Yes or No."
def retry():
user_input = raw_input("Would you like to face %s again? (Yes/No)" % (Enemy)).lower()
if user_input == "yes":
respawn()
getMove()
elif user_input == "no":
print "Thanks for playing!"
else:
print "Please enter either Yes or No."
Change your if condition to
user_input in ["Yes", "yes"]
Reason: When you write user_input == "Yes" or "yes", it evaluates as:
(user_input == "Yes") or "yes"
The second part of OR is a True always(non-zero length string). Hence your problem of if block executing always.
I'm trying to kill a program in python 3 when the user says 'no' to start a maths quiz, : here is the code I'm using
import sys
while True:
new_item = input("> ")
if new_item == "Yes" or "yes":
break
elif new_item == "no":
sys.exit()
that doesn't work any pointers?
Your problem is here:
if new_item == "Yes" or "yes":
You need to use either:
if new_item in ["Yes", "yes"]:
or:
if new_item == "Yes" or new_item == "yes"
Your original code is parsed as:
if (new_item == "Yes") or "yes":
and this always evaluates to True since "yes" is a true value.
if new_item == "Yes" or "yes":
This conditional is always True. It may be stated as:
(new_item == "Yes") or ("yes")
Non-empty string 'yes' is always evaluated to True.
Change conditional to:
if new_item in ['Yes', 'yes']:
You need to change your if statement, it isn't evaluating properly. You need to use this code to fix your problem:
import sys
while True:
new_item = input("> ")
if new_item == "Yes" or new_item == "yes":
break
elif new_item == "no":
sys.exit()
What about
import sys
while True:
new_item = input("> ")
new_item = new_item.lower()
#everything you wrote in input will be lowercase, no more "or" problems
if new_item.lower() == "yes":
break
elif new_item.lower() == "no":
sys.exit()
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 7 years ago.
Well my code is working, but when I type No if I want to retry to enter the password it doesn't work; it just goes to the enter password line (line 20). I have tried multiple ways to fix this but I simply cannot.
import time
import os
print ("Hello world.")
time.sleep(1)
print ("Waiting 5 seconds.")
time.sleep(5)
print ("You have waited 10 seconds.")
print ("Executing Chrome.")
time.sleep(1)
print ("Execution failed!")
password = input("Enter the execution password: ")
if password == 'password1234':
os.system ('C:\\Users\\Harry\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe')
else:
print ("Wrong password!")
time.sleep(1)
passretry = input("Do you want to try again? ")
if passretry == 'yes' or 'Yes':
passretry1 = input("Enter password: ")
if passretry1 == 'password1234':
os.system ('C:\\Users\\Harry\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe')
elif passretry == 'no' or 'No':
print ("Closing...")
time.sleep(1)
else:
print ("Wrong password.")
time.sleep(.5)
print ("Retry limit exceeded, closing.")
time.sleep(1)
if passretry == 'yes' or 'Yes':
the above if statement is evaluated as: -
if (passretry == 'yes') or 'Yes':
Now, since 'Yes' is evaluated to True, so, your if statement is always True, and hence you always have to enter new password.
You need to change the condition to: -
if passretry in ('yes', 'Yes'):
likewise, the following elif should be changed to: -
elif passretry in ('no', 'No'):
This condition:
if passretry == 'yes' or 'Yes':
means "If passretry == 'yes' is true, or 'Yes' is true". 'Yes' is always true, because a non-empty string counts as true. That's why you're always taking the first code path.
You need to spell things out a little more:
if passretry == 'yes' or passretry == 'Yes':
(Or to make your code a bit more general:
if passretry.lower() == 'yes':
which would allow for people shouting YES.)
You need another complete statement:
passretry == 'yes' or passretry == 'Yes':
The string 'Yes' always evaluates to True.