0

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!')
1
  • Check the syntax of the or statement. Do you mean if s == a or s == b? Commented Sep 26, 2019 at 15:28

5 Answers 5

4

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'):
Sign up to request clarification or add additional context in comments.

Comments

2
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

Comments

1

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.

Comments

0

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/

Comments

-3

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!')

2 Comments

This isn't right either. The expression is equivalent to ((FavPokemon == 'Pikachu') or 'pikachu') which will always be true - see docs.python.org/3/reference/….
yess sorry there was a silly mistake i did at the beginning. I edited the code again. Thanks for telling me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.