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'")
Related
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 1 year ago.
Background:
I'm experimenting with while loops and I just made a simple program nested in a while loop.
Code:
while True:
userinput = input("Is nem jeff? ")
if userinput == "y" or "yes" or "Y" or "Yes":
print ("YAY!")
elif userinput == "n" or "no" or "N" or "No":
print ("das sad :(")
else:
print ("wrong input")
break
Problem:
My program should be looping until the user types in an invalid input, but instead, it always returns the value nested in the if statement, no matter what I type in. What am I doing wrong?
Your conditionals aren't doing what you think they are.
In Python, a non-zero-length string evaluates to True in a boolean
context. The or operator performs a boolean or operation between
the lefthand operand and the righthand operand.
So when you write this:
if userinput == "y" or "yes" or "Y" or "Yes":
What you are saying is this:
if (userinput == "y") or True or True or True:
Which will always evaluate to True. You want to write instead:
if userinput == "y" or userinput == "yes" or userinput == "Y" or userinput == "Yes":
Or more simply:
if userinput.lower() in ['y', 'yes']:
The reason false down to truthsy or falsy in if conditions.
based on your initial code block
print('y'=='yes'or'y')
[output] y
print('n'=='yes'or'y')
[output] y
based on the above you can see that regardless of you input, your first if statement would be evaluated as True.
rather than doing that, try doing this instead.
while True:
userinput = input("Is nem jeff? ").lower()
if userinput in ['yes','y']:
print ("YAY!")
elif userinput in ['no','n']:
print ("das sad :(")
else:
print ("wrong input")
break
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 6 months ago.
The following code snippet is meant to allow the user to input the answer to a question. They are allowed to enter four
answers: either y or Y for “yes”, or n or N for “no”. The program is supposed to print out the received answer if the
entry is valid, and print out an error message otherwise.
answer = input("What is your answer? ")
if answer == "y" or "Y":
print("You answered yes")
elif answer == "n" or "N":
print("You answered no")
else:
print("You didn’t enter an acceptable answer")
It just keeps on saying that I answered yes regardless if I put n or N or some random thing. Can someone explain it to me?
The precedence of the or is not what you are expecting. Instead try:
answer = input("What is your answer? ")
if answer in ("y", "Y"):
print("You answered yes")
elif answer in ("n", "N"):
print("You answered no")
else:
print("You didn’t enter an acceptable answer")
Or maybe like:
answer = input("What is your answer? ")
if answer.lower() == "y":
print("You answered yes")
elif answer.lower() == "n":
print("You answered no")
else:
print("You didn’t enter an acceptable answer")
Your first condition will always return true because "Y" is always truthy.
Try: if answer == "y" or answer == "Y":
And the same modification for the other conditional.
If you want to check a variable shortly as you want, use something like this:
if answer in ["y","Y"]:
It will return True if answer is y or Y
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"]
This question already has answers here:
Python And Or statements acting ..weird
(5 answers)
Closed 6 years ago.
How can I make sure the user input only 'y' or 'n' in and make the program only accept 'y' or'n' as an answer?
while True:
try:
cont = input("Do you want to continue? If so enter 'y'.")
if cont != "n" or cont !="y":
print("Please enter 'y' or 'n'")
else:
break
except ValueError:
print("Please enter 'y' or 'n'")
else:
break
A condition of the type if cont != "n" or cont !="y": will always be true as cont can not be n and y at the same time.
You should use and operator instead of or operator. To avoid such confusion, you can try and write such conditions in close to english way, like this
cont="a"
if cont not in ("n", "y"):
print "Welcome"
This can be read as "if cont is not one of...". The advantage of this method is that, you can check for n number of elements in a single condition. For example,
if cont not in ("n", "y", "N", "Y"):
this will be True only when cont is case-insensitively n or y
Edit: As suggested by Eric in the comments, for single character checking we can do something like this
if cont not in "nyNY":
Because it should be if cont != "n" and cont !="y":. Every word is either not n or not y.