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 9 months ago.
I am trying to call a python function inside of an if when you input a certain letter. Even if the letter is not meant to be noticed by the if statement it calls both functions.
if printWrite == 'p' or 'P':
printNum()
if printWrite == 'w' or 'W':
writeNum()
Always calls both functions.
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 12 months ago.
I'm a beginner in Python and I'm trying to write an IF statement with multiple OR conditions in the expression. My problem is that the expression is only "True" for the first OR condition.
def rna_length(mrna):
if (mrna[-3:]) == ("UGA" or "UAA" or "UAG"):
return print("TRUE!")
else:
return print("False")
rna_length('AUGAGGCACCUUCUGCUCCUUAA')
I was expecting True after running the code but the code is printing False.
Kindly help me understand my mistake.
Here is how to do:
if mrna[-3:] in ("UGA", "UAA", "UAG"):
("UGA" or "UAA" or "UAG") will return "UGA" because the operator or return the first non-False value. Since non-empty strings arn't False the first value is returned.
This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Is there a difference between "==" and "is"?
(13 answers)
Closed 3 years ago.
I'm using the pow function and found out I had a bug in my code because == and is were not having the same behavior.
Here goes an example: pow(3, 47159012670, 47159012671) == 1 returns True but pow(3, 47159012670, 47159012671) is 1 returns False.
I'd like to know what is it that I'm not getting.
The difference between is and == is:
a1 is a2 # return True
only when they share the same location or id in memory, that means when id(a1) and id(a2) are the same. I hope this will help more.
This question already has answers here:
How to simplify multiple or conditions in an if statement?
(1 answer)
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 5 years ago.
while MenuChoice != '1' or '2' or '3':
MenuChoice = input('What would you like to do? (Type a number):')
This is the code I have at the moment, however, when the code is executed, the MenuChoice user input is repeated no matter what character is entered (this includes 1, 2, and 3)
How can I make it so that the program checks if the user's input for MenuChoice (prior to this section to code) was '1', '2', or '3' and if not, repeat the MenuChoice input?
Thank you in advanced.
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 5 years ago.
beginner question here:
I want to check if certain keys in a dict have a certain value
simplified version of what I tried:
d = {'a':'b', 'b':'b', 'c':'c'}
def b_check():
if d['a'] and d['b'] and d['c'] == 'b':
return True
else:
return False
However, I always get True as the output.
Thanks in advance for the help :)
This question already has answers here:
Why does comparing strings using either '==' or 'is' sometimes produce a different result?
(15 answers)
Closed 9 years ago.
If I understand correctly, the is operator can take the place of ==.
Why when I write
if inpty == "exit":
return
does the function exit, but when I write
if inpty is "exit":
return
the function does not?
inpty is the value of the input.
is compares identity, whereas == compares equality.
In other words, a is b is the same as id(a) == id(b).
because in this case, the is operator is testing identity, not the value.