Elif Statement
Sometimes a program needs to handle more than 2 possibilities (if
, else
). We can add more conditions using the elif
statement. For example, in soccer, either the home team wins, the away team wins, or it's a draw:
homeGoals = 3
awayGoals = 2
if homeGoals < awayGoals:
print("The away team won!")
elif homeGoals > awayGoals:
print("The home team won!")
else:
print("It's a draw!")
Here we can see one elif
statement, but we can add as many as we want.
It's important to realize that conditions are evaluated from top to bottom. This means the block of the first condition that evaluates to true (True
) will be executed. In the example above, homeGoals < awayGoals
is false, and homeGoals > awayGoals
is true, so the block of the first elif
statement is executed, and the rest of the conditions are skipped. If no condition is true, the block of the else
statement is executed.
Instructions
Use an elif
statement to print Just Do It
if the brand is "Nike"
.
Start programming for free
6/10