This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
I'm constructing an interactive timetable for terminal with Python, but at the end of my code where i have if, elif and else statements no matter what user input i give it keeps passing the if statement. Any Solutions would be greatly appreciated and Thank You for your time :)
while True:
TimeTable()
print "\nDo you wish to go again? "
answer = raw_input()
if answer == "Yes" or "yes":
print " "
continue
elif answer == "No" or "no":
print "Ok then"
break
else:
print "Ok then"
break
answer == "Yes" or "yes"
# is the same as
(answer == "Yes") or "yes"
and is always True. You can solve your problem this way:
answer in ["Yes", "yes"]
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)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 17 days ago.
I have written this very simple code. I am new to programming. But I seem to dont understand why this loops doesnt break when I give it "N" for an answer when I run it.
while (True):
name = input("What is your name?\n")
print(f"Hello {name}, how are you today?")
answer = input("would you like to continue with the conversation? Reply with 'Y' or 'N'\n")
if answer == "y" or "Y":
continue
elif answer == "N" or "n":
break
else:
print("Kindly only answer with 'Y' or 'N'")
I wanted this to get out of loop and break the program when I enter "N"
You need to include answer == in both sides of the conditionals.
i.e.
while (True):
name = input("What is your name?\n")
print(f"Hello {name}, how are you today?")
answer = input("would you like to continue with the conversation? Reply with 'Y' or 'N'\n")
if answer == "y" or answer == "Y":
continue
elif answer == "N" or answer == "n":
break
else:
print("Kindly only answer with 'Y' or 'N'")
This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 6 days ago.
I am trying to create a small bit of code in Python that runs a conversation, then asks a Y/N question i.e "Do you believe the sky is blue?"
When I run the code, it works well until I reach the question. It ignores my parameters for Y/N answers to print specific responses. It gives me the print response attached to my "else" statement.
I am confused if I am writing my If/elif/else statements wrong?
My code is written as follows:
x = input('do you believe the sky is blue')
if x == "yes":
print("I believe you are correct")
elif x == "no":
print('I think you have a unique perspective.')
else:
print('Please answer Yes or No.)
You use .lower() to obtain a lower case version of a string, and you are missing a ' in the last print():
x = input('do you believe the sky is blue ').lower()
if x == "yes":
print("I believe you are correct")
elif x == "no":
print('I think you have a unique perspective.')
else:
print('Please answer Yes or No.')
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
Just learning how to code, and wanted to make a small program to see what I know.
n = int(input("Pick a number any Number: "))
if n > 100:
print ("No... Not that Number")
else:
answer = input("Would you like to know your number?")
if answer == "Y" or "Yes" or "y" or "yes":
print ("Your number is %s" % (n))
elif answer == "N" or "No" or "n" or "no" or "NO":
print ("Oh, well that's a shame then.")
else:
print ("Please type Yes or No")
input("Press Enter/Return to Exit")
Everything works, except for the second if statement, which doesn't follow any of the data entered into input. Any reason why it does this?
Python isn't human, it doesn't understand
if answer == "Y" or "Yes"
The way you mean it to. You should do
if answer == 'Y' or answer == 'Yes'
Or even better
if answer in ('Yes', 'Y', 'yes', 'y')
Or even shorter
if answer.lower() in ('yes', 'y')
== has a higher precedence than or. So, in the if condition your're checking whether answer == 'Y' and then oring this boolean expression with "Yes", which is a non-None string, so it evaluates as True. Instead, you should use the in operator to check if answer is one of the values you're interested in:
if answer in ("Y", "Yes", "y", "yes"):
print ("Your number is %s" % (n))
elif answer in ("N", "No", "n", "no", "NO"):
print ("Oh, well that's a shame then.")
else:
print ("Please type Yes or No")
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 my program keeps rolling the dice again regardless of the users answer, and it doesn't print the goodbye message.
I'll include the code below so you can test yourself (Python 3)
from random import randint
def rollthedice():
print("The number you rolled was: "+ str(randint(1,6)))
rollthedice()
shouldwecontinue = input("Do you wish to roll again? (y or n) ").lower()
if shouldwecontinue == "y" or "yes":
rollthedice()
elif shouldwecontinue == "n" or "no":
print("Goodbye.")
elif shouldwecontinue != type(str):
print("Sorry the program only accepts 'y' or 'n' as a response.")
The desired effect of the program should be that if the user enters y, it rolls the dice again, respectively starting the whole program again. Whereas, if the user enters no then it should print the Goodbye message.
or doesn't work like that. Right now your program is evaluating (shouldwecontinue == "y") or "yes", and since Python interprets "yes" as truthy, then that condition always succeeds. The simplest fix is to change it to shouldwecontinue == "y" or shouldwecontinue == "yes". And then similar changes for the other conditional expressions, of course.
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
I've just started using python and have got stuck on something that, in my mind, clearly should work. This is my first code and I just try to have a conversation with the user.
year = input("What year are you in school? ")
yearlikedislike = input("Do you like it at school? ")
if (yearlikedislike == "yes" or "Yes" or "YES" or "yep" or "yup" or "Yep" or "Yup"):
print("What's so good about year " + year, "? ")
input("")
print("That's good!")
time.sleep(1)
endinput = input("I have to go now. See you later! ")
exit()
if (yearlikedislike == "no" or "No" or "nope" or "Nope" or "NOPE"):
print("What's so bad about year " + year, "?")
input("")
time.sleep(1)
print("Well that's not very good at all")
time.sleep(1)
endinput = input("I have to go now. See you later! ")
time.sleep(1)
exit()
My problem is that even if I reply with a negative answer it will still reply with a response as if I have said yes and if I switch the 2 around (so the code for the negative answer is above the code for the positive answer) it will always reply as if I have given a negative response.
if yearlikedislike in ("yes", "Yes", "YES", "yep", "yup", "Yep", "Yup"):
or
if yearlikedislike.lower() in ("yes","yep","yup"):
will do the trick
This is because Python is evaluating the "truthiness" of "Yes".
Your first if statement is interpreted like this:
if the variable "yearlikedislike" equals "yes" or the string literal "Yes" is True (or "truthy"), do something
You need to compare against yearlikedislike each time.
Try it like this:
if yearlikedislike in ("yes", "Yes", "YES", "yep", "yup", "Yep", "Yup"):
#do something
if (yearlikedislike == "yes" or "Yes" or "YES" or "yep" or "yup" or "Yep" or "Yup"):
Strings evaluate to True. I know you think you're saying that if yearlikedislike is equal to any of those things, keep going. However, what you're actually saying is:
if yearlikedislike equals "yes", or if "Yes" exists (which it does), or "YES" exists, etc:
What you want is either:
if (yearlikedislike == "yes" or yearlikedislike == "Yes" or yearlikedislike == "YES")
or better:
yearlikedislike in ("yes", "Yes", "YES", "yep", "yup", "Yep", "Yup")
It is because the condition is interpreted as :
if(yearlikedislike == "yes" or "Yes" == True or "YES" == True #...
try
if(yearlikedislike == "yes" or yearlikedislike == "Yes" or yearlikedislike == "YES"#...
or a more concise way :
if(yearlikedislike in ("yes", "Yes", "YES", #...
even more concise way :
if(yearlikedislike.lower() in ("yes", "yup", #...
A String (here "Yes") converted to a boolean is converted to True if it's not empty
>>> bool("")
False
>>> bool("0")
True
>>> bool("No")
True
Each part after or is independant from the previous.
Also consider using else or elif instead of two related if. And try to lower character before testing them so you need less test.