This question already has answers here:
How to check variable against 2 possible values?
(6 answers)
Closed 4 years ago.
I am trying to program an ATM machine with this code in python. But regardles of what is inputted, it just says that the card is successfully inputed.
inputCard = input("Welcome to the atm machine, please insert your credit card (Type 'Yes' when you have done so) ")
if inputCard == ['No', 'no']: #checks if card has been entered
print ("Please retry")
else:
print ("Card is successfully inputed") `
Thanks
The equality operator == compares whether the input, which is a string, equals the right hand side, which is a list. Intuitively, a list will never equal a string.
So, use the in operator to see if the answer is in the possible options:
if inputCard in ('No', 'no'):
Alternatively, convert the answer to lowercase and then use ==:
if inputCard.lower() == 'no'
This way will accept no, No, NO and nO.
You are comparing the "inputCard" to a list. Try:
if inputCard.lower() == "no":
inputCard is str,["NO","no"] is list.They will not equal.And you can try like this
if inputCard.lower() == 'no':
or
if inputCard.upper() == 'NO':
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
here is the code I am trying to fix:
the program runs but never returns covid negative even if I answer with a no, what would the correct syntax be to write this statement?
elif question1 and question2 and question3 and question4 and question5 and question6 == "NO":
print(name)
print(gender)
print(number)
print(adress)
print("COVID-19 Negative")
neg_counter = neg_counter + 1
Your code actually won't properly run at all – it'll compute as something like this:
(((question1 and question2) and question3) and question4)...
As in it's not actually calculating what I think you want, which is to see if any of them are =="NO".
One way to do this is:
elif "NO" in (question1, question2, question3, question4, question5, question6):
EDIT:
Since OP has clarified and wants to check if all of them are =="NO" or not, we can do this:
elif question1 == question2 == question3 == question4 == question5 == question6 == "NO":
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 4 years ago.
I'm trying to make this code work but I can't seem to find the right solution.
while True:
tname = input("Please enter the unit of the temperature: ")
list=["Celsius","Kelvin","Fahrenheit"]
if tname == list[0] or list[1] or list[2]:
break
elif tname is not list[0] or list[1] or list[2]:
print("Not a valid unit. Please try again.")
I want the program to stop whenever either Celsius, Kelvin or Fahrenheit is typed but the program stops regardless of what I write. Do you guys know to fix it? Thanks in advance
The technically correct answer is that chaining comprehensions doesn't work like that; you have to do
if tname == list[0] or tname == list[1] or tname == list[2]:
But have you considered using in?
if tname in list:
or similarly:
if tname not in list:
Also, I'd advise against using list as the name of your list, as that's also the name of a type!
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
some assistance is needed:
I've been having some trouble with the OR operand ...I understand how it works ( either or condition must be true), but i'm not sure why it is behaving in the fashion explained below.
The 'or' statement doesn't seem to be checking the second condition in my "flowContol' function.. when I enter 'y' it sees the condition as true and runs without any problems... however when i enter 'yes' it evaluates the condition as false...
-I excluded the other functions-..
def flowControl():
answer = input("do you want run the 'displayLession' function? ( yes or
no)").strip()
if answer == ('y' or 'yes'):
displayLesson()
else:
userTime()
print('End program')
flowControl()
What you are looking for is
if answer == 'y' or answer == 'yes':
Or
if answer in ['y', 'yes']:
Why? Simple, your code evaluates before what is between parentheses 'y' or 'yes' and this is 'y', because 'y' is a truthy expression in python, so no need to evaluate the latter expression.
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 8 years ago.
I have been having trouble with my python code and have been asking around for help. I have heard mixed things about this website but i have no where else to turn so i was hoping someone on here could show me the light and see if they can find out what's wrong with my code (other then it being messy and inefficient). here it is
D=False
while D==False:
input=raw_input("please type 1 for 10p, 2 for 20p, 3 for 50p, 4 for 1 pound")
while input not in ['1', '2', '3', '4']:
print "That is not a correct coin value"
input = raw_input("input: ")
else:
if input=="1":
m=m+10
if input=="2":
m=m+20
if input=="3":
m=m+50
if input=="4":
m=m+100
print("your current credit in pennys is:")
print(m)
D2=raw_input("are you done")
if D2=="yes" or "Yes":
D=True
else:
D=False
I kind of mucked up the implantation of the code on here but you get the picture. It all works fine until you get to the bit asking you if you are done. For some reason it carries on even if you don't put yes. Can any nice coders out there tell me what's up with it
This
if D2=="yes" or "Yes":
always evaluates to true, as it's essentially parsed like this:
if (D2=="yes") or "Yes":
You probably wanted:
if D2 == "yes" or D2 == "Yes":
Which is better written as:
if D2.lower() == "yes":
Or:
if D2 in ["yes", "Yes"]:
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 9 years ago.
I Am Not A Pro, I Have Not Been Programing For Long But Why Doesn't This Work?
door = input("I Have Found A Haunted House,Should I Go In?")
if door == "yes" or "Yes" or "YES":
print("Ok! I Am Going In")
else:
print("What?")
while door == "no" or "No" or "NO":
print("Awwh Thats A Shame, I Was Getting Excited")
Don't worry, this is a very common mistake.
You need to use in here:
if door in ("yes", "Yes", "YES"):
Or, even better, str.lower:
if door.lower() == "yes":
The reason for this is that Python evaluates non-empty strings as being True. So, your code is actually being interpreted like this:
if (door == "yes") or ("Yes") or ("YES"):
# True/False True True
As you can see, this if-statement will always pass.