Back

Activity 1.11: If, Elif, and Else

divider

Activity 1.11

If, Elif, and Else

Key Concepts

else

elif

else

else runs when the if condition was False — exactly one of the two blocks always runs.

password = input("Enter password:")
secret = "secret123"
if password == secret:
print("Access granted.")
else:
print("Access denied.")

else

Terminal window
Enter password: secret123 [Enter]
Access granted.
Terminal window
Enter password: secretpassword [Enter]
Access denied.

The Problem with Independent ifs

Last activity's Letter Grade Calculator needed a two-part condition for every range, like grade >= 80 and grade < 90. That gets repetitive fast.

elif

elif (short for "else if") chains conditions together — Python checks them in order and runs only the first one that's True.

grade = int(input("Enter grade:"))
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 60:
print("D")
else:
print("F")

elif

Terminal window
Enter grade: 86 [Enter]
B
Terminal window
Enter grade: 49 [Enter]
F

Today's Objectives

  • Writing an if/else statement
  • Chaining conditions with elif
  • Importing the random module

Key Terms

else
Runs when every condition before it was False.
elif
Chains another condition onto an if — only the first true branch runs.
Module
A collection of tools not built directly into Python — you bring it in with import.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-11-if-elif-else.


Task 1: Chinese Zodiac Calendar

  • Use the birth year's remainder after dividing by 12 to determine a zodiac animal, with an elif chain.
If, Elif, and Else
import random
print("--- Chinese Zodiac Calendar ---")
birth_year = int(input("Enter your birth year: "))
zodiac_number = birth_year % 12
zodiac_animal = "" # Assign animal based on birth year
if zodiac_number == 0:
zodiac_animal = "monkey"
elif zodiac_number == 1:
zodiac_animal = "rooster"
elif zodiac_number == 2:
zodiac_animal = "dog"
elif zodiac_number == 3:
zodiac_animal = "pig"
elif zodiac_number == 4:
zodiac_animal = "rat"
elif zodiac_number == 5:
zodiac_animal = "ox"
elif zodiac_number == 6:
zodiac_animal = "tiger"
elif zodiac_number == 7:
zodiac_animal = "rabbit"
elif zodiac_number == 8:
zodiac_animal = "dragon"
elif zodiac_number == 9:
zodiac_animal = "snake"
elif zodiac_number == 10:
zodiac_animal = "horse"
else:
zodiac_animal = "sheep"
print(f"Birth year: {birth_year} - You are the year of the...{zodiac_animal}.")
input("Press enter to continue...")

Task 2: Lottery Game

  • Import the random module to generate two winning numbers.
  • Compare the player's two picks against them with elif.
If, Elif, and Else
import random
print("--- Chinese Zodiac Calendar ---")
birth_year = int(input("Enter your birth year: "))
zodiac_number = birth_year % 12
zodiac_animal = "" # Assign animal based on birth year
if zodiac_number == 0:
zodiac_animal = "monkey"
elif zodiac_number == 1:
zodiac_animal = "rooster"
elif zodiac_number == 2:
zodiac_animal = "dog"
elif zodiac_number == 3:
zodiac_animal = "pig"
elif zodiac_number == 4:
zodiac_animal = "rat"
elif zodiac_number == 5:
zodiac_animal = "ox"
elif zodiac_number == 6:
zodiac_animal = "tiger"
elif zodiac_number == 7:
zodiac_animal = "rabbit"
elif zodiac_number == 8:
zodiac_animal = "dragon"
elif zodiac_number == 9:
zodiac_animal = "snake"
elif zodiac_number == 10:
zodiac_animal = "horse"
else:
zodiac_animal = "sheep"
print(f"Birth year: {birth_year} - You are the year of the...{zodiac_animal}.")
input("Press enter to continue...")
print("--- Lottery Game ---")
print("Rules: Pick two lottery numbers ranging from 0 to 9")
print("Match two numbers and win BIG!")
print("Match one number in order and win SMALL!")
lotto_num_1 = int(input("Enter your 1st lottery number (0 to 9): "))
lotto_num_2 = int(input("Enter your 2nd lottery number (0 to 9): "))
winning_num_1 = random.randint(0, 9)
winning_num_2 = random.randint(0, 9)
# Exit the program if either lotto number is out of range
if lotto_num_1 < 0 or lotto_num_1 > 9 or lotto_num_2 < 0 or lotto_num_2 > 9:
print("Error: Your lotto numbers are invalid. Exiting")
exit()
print(f"The winning numbers are {winning_num_1} and {winning_num_2}!")
if lotto_num_1 == winning_num_1 and lotto_num_2 == winning_num_2:
print("You win BIG!")
elif lotto_num_1 == winning_num_1 or lotto_num_2 == winning_num_2:
print("You win SMALL!")
else:
print("You lose! Better luck next time!")
divider

Checkpoint

Verify your program works correctly.

Example Output
--- Chinese Zodiac Calendar ---
Enter your birth year: 1990 [Enter]
Birth year: 1990 - You are the year of the...horse.
Press enter to continue...
--- Lottery Game ---
Rules: Pick two lottery numbers ranging from 0 to 9
Match two numbers and win BIG!
Match one number in order and win SMALL!
Enter your 1st lottery number (0 to 9): 2 [Enter]
Enter your 2nd lottery number (0 to 9): 4 [Enter]
The winning numbers are 2 and 6!
You win SMALL!
divider

Reflection

Answer the following questions before submitting your work.

  1. In an if/elif/else chain, how many of the blocks can run at most?
  2. Why didn't the Zodiac Calendar need a final elif zodiac_number == 11: check before its else?
  3. How is importing random here similar to importing math back in Activity 1.9?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete