This question already has answers here:
How to make user input not case sensitive?
(4 answers)
Closed 6 months ago.
I am adding a few ways of checking user input to my code. Instead of writing if Answer == ("Yes") or Answer == ("yes"), is there any way I could make the variable input case-insensitive, so that I don't have to type the word yes twice? Additionally, would there be any way I could make the every variable input option in the line case-insensitive? if Answer == ("Yes") or Answer == ("yes") or Answer == ("Y") or Answer == ("y"):.
def Repeat():
Answer = input ("\nDo you want to play again? (Y/N): ")
if Answer == ("Yes") or Answer == ("yes") or Answer == ("Y") or Answer == ("y"):
print ("")
if Answer == ("No") or Answer == ("no") or Answer == ("N") or Answer == ("n"):
exit()
if Answer == "Options" or Answer == "options" or Answer == "o" or Answer == "O":
print ("")
else:
print ("Invalid Input")
Repeat()
Repeat()
You can change the case of the user input and just check against that:
answer = input("\nDo you want to play again? (Y/N): ").lower()
if answer in ("yes", "y"):
...
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 last year.
I'm currently writing a program for a school project.
The purpose is for it to work as an online cash register.
answer = "a"
qty1 = 0
while True:
answer = str(input("\nDo you really wish to buy this? (Y/N) "))
if answer == "Y" or "y":
qty1 = int(input("\nHow much quantity of this item would you like to buy? "))
print("\nDo you really wish to buy", qty1, "pieces? (Y/N) ")
answer = str(input(""))
if answer == "Y" or "y":
print("Confirming order and returning to menu.")
break
else:
qty1=0
print("Cancelling order and returning to menu")
break
elif answer == "N" or "n":
print("Okay, returning to menu.")
break
else:
print("not valid answer")
Here is the code for the part of the program I'm having trouble with.
Whenever I reach this part of the program, the input seems to ignore whatever I put and it always goes through the if path.
Does anyone know why this is?
I'm new to programming, so sorry if this is just an easy fix.
Here's the working code which needed some formatting:
Changed these lines -> answer in ("Y", "y") and answer in ("N", "n"). This is because if answer == 'Y' or 'y' is used then 'y' will always evaluate to True which means the first if statement always executes. Also, the error in the print statement has a colon: print: - this was corrected.
answer = "a"
qty1 = 0
while True:
answer = str(input("\nDo you really wish to buy this? (Y/N) "))
print(answer)
if answer in ("Y", "y"):
qty1 = int(input("\nHow much quantity of this item would you like to buy? "))
print("\nDo you really wish to buy", qty1, "pieces? (Y/N) ")
answer = str(input(""))
print(answer)
if answer in ("Y", "y"):
print("Confirming order and returning to menu.")
break
else:
qty1=0
print("Cancelling order and returning to menu")
break
elif answer in ("N" ,"n"):
print("Okay, returning to menu.")
break
else:
print("not valid answer")
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:
Why does comparing strings using either '==' or 'is' sometimes produce a different result?
(15 answers)
Closed 5 years ago.
choice = 'yes'
while choice is 'yes':
choice = input("Enter a value for choice : ")
# when i input 'yes' from keyboard for choice, it falls to else block.
if choice is 'yes':
print("As expected")
break
else:
print("Need Help !!!")
I have even tried to match with 'yes\r', as we press 'enter' after our input from keyboard, it still failed to match. Need some insight.
The is keyword is a test for object identity while == is a value comparison. Thus, you needed to change your is to ==.
choice = 'yes'
while choice == 'yes':
choice = input("Enter a value for choice : ")
if choice == 'yes':
print("As expected")
break
else:
print("Need Help !!!")
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 7 years ago.
So, I Am trying to create some code, and for whatever reason the following always gives the same output, "Great! let's continue"
I am fairly new programmer, so any explanation has about why this is happening and how I can fix it is welcome. Thank you!
#python 2
UserAnswer = raw_input('Is your age and birthday correct? (yes/no):')
if UserAnswer == 'yes' or "Yes":
print ("Great! let's continue.")
elif UserAnswer == 'no' or "No":
print("check for your error, the program will now end")
SystemExit
else:
print 'Invalid input.'
The following line will always evaluate to True:
if UserAnswer == 'yes' or "Yes"
This is because "Yes", when treated as a boolean, evaluates to True. So the result of the OR will be True. You can fix it by changing it to the following (and do the same thing for the other line)
if UserAnswer == 'yes' or UserAnswer == 'Yes'
Don't do:
if UserAnswer == 'yes' or "Yes":
Do instead:
if UserAnswer == 'yes' or UserAnswer == "Yes":
Same applies to the elif condition
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
When I type no into the input on the terminal. It goes through the if choice == "yes" part.
I want it to go through the else. Please help.
choice=raw_input("Will you help us? Yes or no?")
if choice == "yes" or "Yes":
print "Yeah! You are a hero!"
name = raw_input("What is your name?")
print "Alright, " + str(name) + " ,let's go choose a weapon from the blacksmith."
else:
print "You're a coward. :("
quit()
What's wrong?
The bug is in this line of code:
if choice == "yes" or "Yes":
Python sees this as an "or" of two conditions:
if (choice == "yes") or ("Yes"):
Which is same as:
if (choice == "yes") or True:
because a non-empty string is always True.
And this finally reduces to:
if True:
as "or"ing with True always evaluates to True.
This would give you the desired result:
if choice == "yes" or choice == "Yes":
However, that is considered C-style and the pythonic way of comparing multiple values is:
if choice in ("yes", "Yes"):
But in this case, you just want to do a case-insensitive match. So the right way to do that would be:
if choice.lower() == "yes":
And this would even handle odd capitalization in inputs like "yEs" or "YEs".
choice=raw_input("Will you help us? Yes or no?")
if choice == "yes" or choice == "Yes":
print "Yeah! You are a hero!"
name = raw_input("What is your name?")
print "Alright, " + str(name) + " ,let's go choose a weapon from the blacksmith."
else:
print "You're a coward. :("
quit()
The above is the correct format. You did not have the logic set up correctly. Note the following:
a = 1
if a == 2 or 3 :
print 'OK'
It prints 'OK'. Why?
The reason is that python values are evaluated in a left to right fashion. If any value is true then that value is returned. However if all values are false then the last value is returned, in your case 'Yes'. This is what is causing you problems as far as I understand it. You need basically two 'or' conditions, not one....