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.
So the problem I'm having is with this part of a simple script for a Chat Bot about Programming. But when I test this part of the script and type in 'B' as my input, it says ('Panda: I'm glad you share my enthusiasm.') which is what is mean't to happen if I input A! Can someone please point out to me what I'm doing wrong?
invalidAnswer2 = true
while invalidAnswer2 == true :
answer2 = input (user)
if answer2 == ('a') or ('A') :
print ('Panda: I'm glad you share my enthusiasm.')
invalidAnswer2 = false
elif answer1 == ('b') or ('B') :
print ('Panda: I wish. I am actually British, but I dream of going to America!')
print ('Panda: *Cough* Um anyway that was rather unproffesional of me.')
invalidAnswer2 = false
else :
invalidAnswer()
invalidAnswer2 = true
Thank You for helping me fix it. It was a simple typo after all that XD
The problem is your that your first if statement is always True:
if answer1 == "a" or "A"
Will always yield true, because it's really asking:
if (answer1 == "a") or ("A")
And "A" will always return True.
What you meant is probably:
if answer1 == "a" or answer1 == "A"
or the better option:
if answer1.lower() == "a"
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 1 year ago.
For my studies, I have begun to learn the Python Programming Language.
Fairly new to programming and having some trouble understanding why this won't work.
while compReady == False:
compAI = input("Which strategy for the computer [1,2,3]? ")
if compAI == "1" or "2" or "3":
compReady = True
elif compAI != "1" or "2" or "3":
print("Please enter either 1, 2, or 3.")
The problem I have is that no matter what is inputted into compAI, it runs through the 'if' statement. Any help is appreciated, thanks.
if condition should be written as ..
if compAI == ("1" or "2" or "3"):
In your code, Python interpreter will treat "2" and "3" as true. Please note it is not compared with compAI as per if statement syntax.
This question already has answers here:
Why doesn't this "is not" if statement work?
(2 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
I have this code:
choose = ""
while choose != "x" or choose != "X" or choose != "o" or choose != "O":
choose = input("X or O? -> ")
but it continues even if the user insert x, X, o or O.
I am new at coding, anyone have an idea to make it work?
Try this
while choose not in [ "x" , "X" , "o" ,"O"]:
Any of 4 condition becoming true continues the loop. You can use 'and' insted of 'or'.
Instead, try using 'in', 'not in' keywords of Python.
the logic combination is not what you want.
[enter image description here][1]
if you input O for choose, the condition choose != "x" is true, then while loops.
I guess what you want is:
choose = ""
while not(choose == "x" or choose == "X" or choose == "o" or choose == "O"):
choose = input("X or O? -> ")
it works~/:D
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 3 years ago.
Currently working on my first program and my Else statement isn't working in the following code:
info = input("<Y/N>")
if (info == "Y" or "y"):
print("Calculating...")
else:
raise SystemExit
The program just continues to
print("Calculating...")
even if the input isn't "Y" or "y"
if (info == "Y" or "y"):
is equivalent to saying
if ((info == "Y") or ("y"))
Since y is not zero, null, or equivalent, it evaluates to true.
The either of the following will do:
if (info == "Y" or info == "y")
if (info in ("y", "Y"))
if (info.lower() == "y")
You should write something like this:
info = input("<Y/N>")
if (info == "Y" or info == "y"):
print ("Calculating...")
else:
raise SystemExit
In your statement:
info == "Y" or info == "y"
"y" is always True
What's happening is that the or operator is treating the left and right side as boolean (True and False) statements. What it's looking to see on the left side is info == "Y" which might be true or false depending on the input. On the right side it's seeing "y" and checking if "y" is true. In python non-empty strings count as true when evaluated as booleans. To fix this here are two ways to check.
if info == "Y" or info == "y":
if info in ["y", "Y"]:
Let me just add this to the above answers: a more pythonic way of checking
if (info == 'Y' or info == 'y')
is done by
if info.lower() == 'y'.
Of course, info needs to be a string.
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:
String comparison in Python: is vs. == [duplicate]
(4 answers)
Closed 8 years ago.
Whenever I run this I get the third option when it should be returning the first, since s = 'yes'. What is going wrong here?
def shut_down(s):
if s is 'yes':
return 'Shutting down...'
elif s is 'no':
return 'Shutdown aborted!'
else:
return "Sorry, I didn't understand you"
ans = 'Yes'
s = ans.lower()
shut_down(s)
Change
if s is 'yes':
to
if s == 'yes':
and
elif s is 'no':
to
elif s == 'no':
While is is a valid operator, it is not the one to use here (it compares object identity instead of comparing the character sequences).
is tests for identity, not equality. To test if a string is equal to yes use s=='yes'