Mamba Wilbury
Mamba Wilbury

Reputation: 33

How do I stop my Python if statement from printing with my else statement?

I'm just learning python and am having trouble understanding why my if input is triggering my else statement. I'm sure I'm missing something basic here but would like someone to look at it! Essentially, when I am inputting one of my variables it is dragging the else statement into it. I'm attaching the code, thanks for having a look!

n = 'Nike'
p = 'Puma'
a = 'Adidas'

boot = input('What is your favorite boot?')

if boot == n:
  print('Nike, great choice')
if boot == a:
  print('Adidas, not my favorite')
if boot == p:
  print('Not sure about Puma')
else:
  print('I am not familiar with that brand')

Typing in Nike on the input prints

Nike, great choice.
I'm not familiar with that brand.

Upvotes: 2

Views: 1829

Answers (5)

M Hamza Razzaq
M Hamza Razzaq

Reputation: 472

When you enter Nike to the input in the first it checks boot == n which becomes true and it prints "Nike, great choice". Everything is good yet.
After that it checks boot == a which becomes false so it does not print anything After that it checks boot == p which is also false so it goes to the else block or the third if and prints "I am not familiar with that brand". What you need to understand is elif statement so if anyone of the if statements gets true it will skip the rest and do not go the rest of the elif or else block. Here is the correct code

n = 'Nike'
p = 'Puma'
a = 'Adidas'

boot = input('What is your favorite boot?')

if boot == n:
  print('Nike, great choice')
elif boot == a:
  print('Adidas, not my favorite')
elif boot == p:
  print('Not sure about Puma')
else:
  print('I am not familiar with that brand')

Upvotes: 1

Uday
Uday

Reputation: 1

Please try eif, apart the first If, try to change remaining If or else to elif and try.

Upvotes: 0

Felix
Felix

Reputation: 82

You are creating three independent if statements here. Have a look at the attached pseudo code with parentheses

if(boot == n){
   print('Nike, great choice')
}

if (boot == a){
   print('Adidas, not my favorite')
}

if (boot == p){
  print('Not sure about Puma')
}
else{
  print('I am not familiar with that brand')
}

You need to use "elif":

if boot == n:
    print('Nike, great choice')
elif boot == a:
   print('Adidas, not my favorite')
else:
   print('I am not familiar with that brand')

Upvotes: 1

SublimizeD
SublimizeD

Reputation: 134

Currently your input is reading 'What is your favorite boot?' I would print the prompt prior to the input just like the below code

n = 'Nike'
p = 'Puma'
a = 'Adidas'
print('What is your favorite boot?')
boot = input()

Right now the else is only tied to the last if statement try using the Elif statement to tie them all together (below)

if boot == n:
  print('Nike, great choice')
elif boot == a:
  print('Adidas, not my favorite')
elif boot == p:
  print('Not sure about Puma')
else:
  print('I am not familiar with that brand')

Output:

What is your favorite boot?

Nike
Nike, great choice

Upvotes: 0

glglgl
glglgl

Reputation: 91039

Well, what happens e. g. if boot equals to n? The execution goes from up to down and does all tests:

if boot == n:
  print('Nike, great choice')

boot == n. Printed.

if boot == a:
  print('Adidas, not my favorite')

boot != a, nothing printed.

if boot == p:
  print('Not sure about Puma')
else:
  print('I am not familiar with that brand')

boot != p, else part executed.

In order to suppress further tests if a match was made, use elif:

if boot == n:
  print('Nike, great choice')
elif boot == a:
  print('Adidas, not my favorite')
elif boot == p:
  print('Not sure about Puma')
else:
  print('I am not familiar with that brand')

Upvotes: 7

Related Questions