If-Elif-Else Statements In Python

Hi. This page will quickly go over if-elif-else statements in Python. This page is a follow up to If-Else Statements In Python.

Reference: Starting Out With Python – Second Edition By Tony Gaddis

 


If-Elif-Else Statements In Python

In the if-else statement there were two choices. If a condition was met, the code under the if was executed and if the condition was not met, the code under the else part was executed.

With the If-Elif-Else statements, more conditions can be inserted. These extra conditions are associated with elif (short for else if).

The format of the if-elif-else statement in Python is as follows:

 

Example

This example is influenced by the TV game show The Price Is Right. The contestant guesses the price of the car. There are three scenarios:

  1. The guess is correct.
  2. The guess is too high.
  3. The guess was lower than the car’s price.

 

 

 

Leave a Reply