Python Classroom
  • Introduction
  • About Python Classroom
  • Python Cloud Options
    • CS50 IDE
      • CS50 IDE Overview
      • CS50 IDE and Python
      • CS50 IDE Debugging
      • CS50 IDE Shortcuts
      • Linux Bash Shell
      • Vim Mode
        • Vim Tutorial
    • CS50 Sandbox
    • PythonAnywhere
    • Repl.it
  • Python Curriculum Map
    • Pedagogy
      • Python Professional Development
      • Teaching Tips
      • Assessment Tips
      • Rubrics
      • Activities
        • Picture Activity
        • Map Activity
        • Crossing the Bridge
        • NIM
        • Mastermind
        • Cards
          • Card Deck Algorithms
          • Sorting Cards
        • River Crossing
          • Jealous Boyfriends
          • Cannibals and Priests
          • Family
          • Police and the Thief
          • Humans and Monkeys
          • Moving Money
        • Crossing the River
        • Traveling Salesperson
        • Logic Problems
        • CIA Crack the Code
        • IQ Test
        • Puzzles
    • AP Computer Science Principles Framework
  • Python Philosphy
    • How to Practice Python
  • microbit
  • Turtle Graphics
    • Turtle Examples
    • Turtle Activities
    • Turtle Maze Problems
    • Turtle Graphics with loops
    • Turtle Snake
    • Turtle Graphics with Conditionals
  • Output
    • Output Examples
    • Output Mistakes
  • Variables
    • Variable Data Type Examples
    • Variable Role Examples
    • Variables Mistakes
    • Variables Problems
  • Math
    • Math Examples
    • Math Mistakes
    • Math Problems
    • Math Self Check
  • Input
    • Input Examples
    • Input Mistakes
    • Input Problems
  • Decisions
    • if
      • if Examples
      • if Mistakes
      • if Problems
    • if else
      • if else Examples
      • if else Problems
      • if / if else Problems
    • if elif else
      • if elif else Examples
      • if elif else Problems
    • nested if
      • nested if Examples
      • nested if Problems
    • Logical Operators
      • Logical Operators Examples
    • Adventure Game Project
  • Loops
    • while loop - count up
      • Examples
      • Problems
    • while loop - countdown
      • Examples
      • Problems
    • while loop - sentinel value
      • Problems
    • while loop - sentinel menu
      • Problems
    • for loop - range (one argument)
      • Examples
      • Problems
    • for loop - range (two arguments)
      • Problems
    • for loop - range (three arguments)
      • Problems
  • Lists
    • Lists - Numbers
      • Problems
    • Lists - Strings
      • Problems
      • Shopping Project
  • Dictionaries
  • String Methods
  • Functions
    • Variable Scope
    • Functions - no parameters
    • Functions - one parameter
    • Functions - return
    • Functions - lists
  • Files
  • Classes
    • Inheritance
  • Python Projects
    • Adventure Game
    • Restaurant Project
    • Trivia Game
    • Family Tree Project
  • Raspberry Pi
    • Raspberry Pi Models
    • Raspberry Pi Setup
  • Roblox
  • Glossary
Powered by GitBook
On this page
  • Objective
  • Introductory Problem
  • if elif else
  • Difference between if and if elif else
  • Example 1

Was this helpful?

  1. Decisions

if elif else

Previousif / if else ProblemsNextif elif else Examples

Last updated 5 years ago

Was this helpful?

Objective

After working through this lesson, you’ll be able to

  • Write Python code using if else statements.

Introductory Problem

Write Python code that asks for a number and assigns the input to the variable num.

Write if statements that print the messages:

  • num is positive.

  • num is negative.

  • num is 0.

if elif else

Sometimes a problem will have multiple conditional cases.

Difference between if and if elif else

  • Python will evaluate all three if statements to determine if they are true.

  • Once a condition in the if elif else statement is true, Python stop evaluating the other conditions.

  • Because of this, if elif else is faster than three if statements.

The else is not required.

Example 1

Write Python code that asks what type of gas do you want. The average car holds about 17 gallons of gas. Print the total price based on the type of gas and 15 gallons of gas.

Type of Gas

Price per galloon

regular

$3.89

plus

$4.09

premium

$4.19

diesel

$4.59

  • If the user enters “regular”, print the total price

  • If the user enters “plus”, print the total price

  • If the user enters “premium”, print the total price

  • If the user enters “diesel”, print the total price

  • Else if the user enters something else, print invalid gas type

gas = input("What type of gas do you want? ")
if gas == "regular":
    print("Regular Gas:",3.89*17)
elif gas == "plus":
    print("Plus Gas:",4.09*17)
elif gas == "premium":
    print("Premium Gas:",4.19*17)
elif gas == "diesel":
    print("Diesel Gas:",4.59*17)
else:
    print("Invalid Gas Type")