Reputation: 1
Whenever I run the following code, it always runs the else statement, not the if. It works perfectly when I don't have the or operator, but I need to use it. I don't know what to do!
print('What is your favourite Pokemon?')
FavPokemon = input()
if FavPokemon == ('Pikachu' or 'pikachu'):
print('Me too!')
else:
print('Cool! ' + FavPokemon + ' is a good Pokemon. My favourite is Pikachu though!')
Upvotes: 0
Views: 125
Reputation: 1
EXPLANATION OF YOUR MISTAKE
You have done a little syntax mistake in writing the if statement. You have written
if FavPokemon == ('Pikachu' or 'pikachu'):
Here u have kept 'Pikachu' or 'pikachu'
within parenthesis and checked FavPokemon
outside that parenthesis with that ==
assignment operator sign.Doing these causes an error as it is unable to check with the condition.
CORRECT CODE
Correct code will be if you put the whole FavPokemon == 'Pikachu' or FavPokemon =='pikachu'
into a parenthesis.This way it checks that whether FavPokemon
is "Pikachu"
or FavPokemon
is "pikachu"
and gives you the correct result.
The correct code will be:
print('What is your favourite Pokemon?')
FavPokemon = input()
if (FavPokemon == 'Pikachu' or FavPokemon =='pikachu'):
print('Me too!')
else:
print('Cool! ' + FavPokemon + ' is a good Pokemon. My favourite is Pikachu though!')
Upvotes: -3
Reputation: 77932
Programming languages are not natural languages and have different (and much stricter) grammars.
In natural languages, we all understand that "if a is equal to b or c" actually means "if a is equal to a or a is equal to b", but in programming language the formaly similar statement "if a == b or c" is parsed "if (expression) == (other expression)". Then each expression is evaluated (replaced with it's current final value) and only then the equality test is done. So
if a == b or c:
is really:
d = b or c
if a == d:
Now the expression b or c
returns the first of b
or c
which has a true value. Since in Python a non-empty string as a true value, your code:
if FavPokemon == ('Pikachu' or 'pikachu'):
is really:
name = ('Pikachu' or 'pikachu')
if FavPokemon == name:
and since 'Pikachu'
is a non empty strings, this ends up as:
name = 'Pikachu'
if FavPokemon == name:
or more simply:
if FavPokemon == 'Pikachu':
The general rule is that if you want to test against multiple conditions, you have to write it explicitely, ie:
if a == b or a == c:
or
if a == b and x != y:
etc
Now if you want to test whether one object (string or whatever) is part of a collection of objects (tuple, list, etc), you can use the in
operator:
a = 5
if a in (4, 5, 6):
# ...
But in your case, since what you want is to test against the capitalized or uncapitalized version of the same word, you could instead transform the user's input (FavPokemon
) in a "normalized" version and test against the "normalized" version of your string, ie instead of:
if FavPokemon in ('Pikachu', 'pikachu'):
you could write:
FavPokemon = FavPokemon.lower()
if FavPokemon == 'pikachu':
Note that this will be a bit less strict than the in
version as it will accept "pIkachU" or "PIKACHU" or just any combination of upper/lowercase - but for your use case that's usually what's expected.
Upvotes: 1
Reputation: 117
Working Code:
print('What is your favourite Pokemon?')
FavPokemon = input()
if (FavPokemon == 'Pikachu' or FavPokemon == 'pikachu'):
print('Me too!')
else:
print('Cool! ' + FavPokemon + ' is a good Pokemon. My favourite is Pikachu though!')
Useful Links python-or-operator/
Upvotes: 0
Reputation: 308520
The statement isn't working because or
doesn't do what you think it does. The expression ('Pikachu' or 'pikachu')
evaluates to a single string, 'Pikachu'
. The ==
then compares against that only.
What you probably want is:
if FavPokemon in ('Pikachu', 'pikachu'):
Upvotes: 4
Reputation: 7281
print('What is your favourite Pokemon?')
FavPokemon = input()
if FavPokemon == 'Pikachu' or FavPokemon == 'pikachu':
print('Me too!')
else:
print('Cool! ' + FavPokemon + ' is a good Pokemon. My favourite is Pikachu though!')
Is what your code needs to be. Your if statement needs to be changed. The expression before, If x == (a or b)
will always evaluate to true. You were not checking if the FavPokemon
variable was actually Pikachu
or pikachu
, you were checking is the condition (a or b
) is true, which in that syntax, it is always true.
See https://www.w3schools.com/python/python_conditions.asp
Upvotes: 2