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 5 years ago.
I'm just writing a simple if statement. The second line only evaluates to true if the user types "Good!"
If "Great!" is typed it'll execute the else statement. Can I not use or like this? Do I need logical or?
weather = input("How's the weather? ")
if weather == "Good!" or "Great!":
print("Glad to hear!")
else:
print("That's too bad!")
You can't use it like that. The or operator must have two boolean operands. You have a boolean and a string. You can write
weather == "Good!" or weather == "Great!":
or
weather in ("Good!", "Great!"):
What you have written is parsed as
(weather == "Good") or ("Great")
In the case of python, non-empty strings always evaluate to True, so this condition will always be true.
Related
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 6 months ago.
I'm new to python and currently working on implementing Luhns algo in python. I'm already done with calculation of checksum. Now I'm facing challenge in identifying the card company using if and elif statement.
Below is my code block to identify the card company. The problem I'm facing is else if statements are not getting executed even if the conditions are true and, in some cases, even ELSE within IF is not being executed. This happens whenever there's an OR condition to compare with multiple values.
I've tried moving the full conditions within parenthesis((first_2 == (34 or 37) and length==15) but the result is same.
Can't we use multiple conditions in if or elif statement OR is there any other way compare multiple values for if statement?
Thank You
Code>>>
if (digit+number_add2) % 10 ==0 and length >= 13:#check if remainder is Zero
if first_2 == (34 or 37) and length==15:#Check first 2 digits and length
print("AMEX")
elif first_2 == (51 or 52 or 53 or 54 or 55) and length==16:
print("MASTERCARD")
elif first_1== 4 and length == (13 or 16):
print("VISA")
else:
print("INVALID1")#Invalid if number passes Luhns algo but card is not from Visa,MC,Amex
else:
print("INVALID2")#Invalid if card number does not adhere Luhn's algo
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
code = (input("Write a number: "))
print (code)
if code == "1" or "2":
print ("bad")
elif code =="3":
print ("good")
else:
print ("hmmm" )
Output:
Write a number: 3
3
bad
I'm confused, shouldn't the output be "good" instead of "bad"?
The syntax of your logic is wrong:
if code=="1" or code=="2":
What you originally had (if code == "1" or "2") is understood by the interpreter as asking the following two questions:
Does the value in code == "1"?
Is "2" true?
The second question will always evaluate to True. So the if clause will always run.
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 1 year ago.
I wondering why the following tests return False:
Suppose I have 2 simple strings:
str0 = "trade"
str1 = "`trade"
I don't understand why the following tests in python return False:
str1.replace("`", "") is str0
And,
"".join(list(str1)[1:]) is str0 => False
Thanks for your education!
In Python, is compares two objects in memory, == compares their values. Since your two variables are stored at two different places in memory, your comparison using is evaluates to false.
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 years ago.
I was a bit curious if there's something that I was missing in the code below,
The logic I want the user to enter either "Y" or "H" or "E", which I would like to transfer to another class using a return statement. I use the else part to return boolean False if otherwise.
There is a while loop that would send the control back to this same function until the user enters the required value.
On running the program it never enters the else part no matter what input is received.
def getUserIngameOption(self):
userIngameOption=raw_input("Please Enter Y -> for Turn | H -> Hint | E -> to End")
userDesc=userIngameOption.upper()
if not ( userDesc==("Y") or ("H") or ("E")):
print "Mate PLEASE JUST TYPE WHATTTT is asked for !! not this",userDesc
return False
else:
self.userOpt=userDesc
print "This is detail that you got>>>",self.userOpt
return self.userOpt
You arent making the comparison correctly. You are ORing the result of the first comparison with the boolean value of 'H' and 'E'. Since 'H' and 'E' have a boolean value of True, the IF statement will always be False (since you are inverting the condition's result with not).
Change this:
if not (userDesc==("Y") or ("H") or ("E"))
to:
if userDesc not in ("Y","H","E"):
What is happening is that each one is being evaluated separately, you will need to explicitly compare each value.
if not ( userDesc=="Y" or userDesc=="H" or userDesc=="E")
"E" and "H" will evaluate to True in your current code.
This the the correct method :
if not ( userDesc=="Y" or userDesc=="H" or userDesc=="E")
or this :
if userDesc not in ("H","Y","E").
if not ( userDesc==("Y") or ("H") or ("E")) means (userDesc==("Y")) or (("H")) or (("E")) and the second and third terms are always true.
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.
Oh, man! Sorry, I'm quite new to Python. I was just being an idiot. So don't answer this.
in Python I tried to use the if/else function, but it always chose the "if" option. I use Python 2 (IDLE). How can you help? Here is my code:
if answer=="Yes" or "yes":
upload_name=raw_input("What do you want to change it to? ")
elif answer=="No" or "no":
print "Okay. Your name will remain as " + str(real_name) + "."
if answer=="Yes" or answer== "yes":
elif answer=="No" or answer== "no":
Or just use if answer.lower() == "yes"
or "yes" always evaluates to True once the string is a non empty string, you are not comparing to answer, you are testing bool("yes").
In [1]: bool("yes")
Out[1]: True
In your case I would just call lower on the string but if you have multiple values to test against using in can be a nice way to do it:
if answer in {"Yes","yes"}: