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.

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

Python IF, ELIF, and ELSE Statements, In Python, you have the if, elif and the else statements for this purpose. else: Indented statement block for when condition is FALSE Step 2.1 elif( score_practical > 50): print("Please check the input score for 'Practical'. Lets say you wanted to go a step further and display a statement when the total score was actually within the range, i.e. less than 100. This is when the if-else statement would help. Single test: if-else Statement. The if-else statement is used to code the same way you would use it in the English language. The syntax for the if-else statement is:

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

If-else statements in Python, if statement; if-else statement; if-elif-else statement; while loop; for loop Notice that each statement inside the if block is indented by the same amount 2") print( "statement 3") print("Executes every time you run the program")� An example of using the Python else statement. In this example, the else Python statement is given that will execute a line of code if the condition is false. You may also use multiple statements with the same indentation in the else block just like in the if block. See the code and output online: See online demo and code

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

Why is my else statement in python running even if my if statement is , Why is my else statement in python running even if my if statement is true here] i = input() if i in members: print(wow) else: print("no) @Hasan As Elric if you are just learning how to do a switch type statement the keep with� Python If Else in One Line. Python provides a way to shorten an if/else statement to one line. Let’s see how can you do this. The one-liner If-else has the following syntax: # If Else in one line - Syntax value_on_true if condition else value_on_false. See the below example of If-Else in one line.

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

3.1. If Statements — Hands-on Python Tutorial for Python 3, If it is true that the weight is greater than 50, then print the statement about an extra flow of statements, after the if - else statement (whichever block is selected). Note: print() was a major addition to Python 3, in which it replaced the old print statement available in Python 2. There were a number of good reasons for that, as you’ll see shortly. Although this tutorial focuses on Python 3, it does show the old way of printing in Python for reference.

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

Python if..else Statement, In this article, we will go over the basics of the if statement in Python. The STATEMENT block starts with an indentation and ends with the first evaluate to True (10 is greater than 5), and the print function will be executed. We can easily do that using if-else statement. if-else statement # An if-else statement executes one set of statements when the condition is true and a different set of statements when the condition is false. In this way, a if-else statement allows us to follow two courses of action. The syntax of if-else statement is as follows: Syntax:

Python If Else, If Elif Else Statements Explained with Examples, This tutorial explains the syntax and use of Python if else, if-elif-else, nested if, one line A conditional statement (like if X > Y:) envelopes around a block of code. Yes or No >> ").lower() if answer == "yes" : print("You have cleared the test. An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else statement is an optional statement and there could be at most only one else statement following if .

Python if elif else, To handle the situation Python allows adding any If no true condition is found the statement(s) block under else will be executed. In the complex): print("Type of the variable is Complex") elif� You can do these actions with break, continue, and pass statements. Break Statement. In Python, the break statement provides you with the opportunity to exit out of a loop when an external condition is triggered. You’ll put the break statement within the block of code under your loop statement, usually after a conditional if statement.

Understanding Python If-Else Statement, Instead, the statement present outside the if block is executed. if-block. In the code below, both the print statements will be executed since a is� The basic structure of an “if” statement in python is typing the word “if” (lower case) followed by the condition with a colon at the end of the “if” statement and then a print statement regarding printing our desired output. Python is case sensitive too so “if” should be in lower case. Syntax: if <condition>: Print <statement>

Comments
  • Use elif for the middle two branches.
  • You must've posted just as I did. Good description. Got my upvote