Lab 2: Making Decisions

Overview

Programs become powerful when they can make decisions. In this lab, you'll learn how to use conditionals (if, elif, else) to make your programs respond differently based on different situations.

Learning Objectives

By completing this lab, you will be able to:

Prerequisites

Before starting this lab, you should be comfortable with:

Instructions

Part 1: True or False

Python can evaluate statements as either True or False. These are called boolean values.

  1. Try these comparisons in Python:
# Comparison operators
print(5 > 3)    # Is 5 greater than 3?
print(5 < 3)    # Is 5 less than 3?
print(5 == 5)   # Is 5 equal to 5?
print(5 != 3)   # Is 5 not equal to 3?
print(5 >= 5)   # Is 5 greater than or equal to 5?
print(5 <= 3)   # Is 5 less than or equal to 3?

Expected output:

True
False
True
True
True
False
  1. You can store boolean values in variables:
is_raining = True
has_umbrella = False
print("Is it raining?", is_raining)
print("Do I have an umbrella?", has_umbrella)

Part 2: Your First If Statement

An if statement runs code only when a condition is True.

  1. Try this simple example:
temperature = 75

if temperature > 70:
    print("It's warm outside!")

Expected output:

It's warm outside!
  1. Change the temperature to 65 and run again. What happens? (Nothing prints because the condition is False.)

  2. The code inside the if must be indented (4 spaces). This is how Python knows what code belongs to the if:

temperature = 75

if temperature > 70:
    print("It's warm outside!")
    print("Maybe wear shorts!")  # This is also inside the if

print("Have a nice day!")  # This always runs (not indented under if)

Part 3: If-Else

Use else to run different code when the condition is False.

  1. Create a program that gives different advice:
temperature = int(input("What's the temperature? "))

if temperature > 70:
    print("It's warm! Wear light clothes.")
else:
    print("It's cool. Bring a jacket.")

Example interactions:

What's the temperature? 80
It's warm! Wear light clothes.
What's the temperature? 55
It's cool. Bring a jacket.

Part 4: Multiple Conditions with Elif

Sometimes you need to check multiple conditions. Use elif (short for "else if").

  1. Create a grade calculator:
score = int(input("Enter your score (0-100): "))

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
else:
    grade = "F"

print("Your grade is:", grade)

Key insight: Python checks conditions from top to bottom and stops at the first one that's True.

  1. Try different scores: 95, 85, 72, 55. Verify the grades are correct.

Part 5: Combining Conditions

Use and, or, and not to combine conditions.

  1. and - Both conditions must be True:
age = 25
has_license = True

if age >= 16 and has_license:
    print("You can drive!")
else:
    print("You cannot drive yet.")
  1. or - At least one condition must be True:
is_weekend = True
is_holiday = False

if is_weekend or is_holiday:
    print("No school today!")
else:
    print("Time for class.")
  1. not - Reverses True/False:
is_raining = False

if not is_raining:
    print("No umbrella needed!")

Part 6: Putting It Together

Create a program that recommends activities based on weather conditions:

print("=== Activity Recommender ===")

temperature = int(input("Temperature (°F): "))
is_raining = input("Is it raining? (yes/no): ") == "yes"

if is_raining:
    print("Recommendation: Stay inside and read a book!")
elif temperature > 85:
    print("Recommendation: Go swimming!")
elif temperature > 60:
    print("Recommendation: Go for a walk in the park!")
elif temperature > 40:
    print("Recommendation: Perfect weather for a museum visit!")
else:
    print("Recommendation: Bundle up and make hot cocoa!")

Notice the trick on line 5: input(...) == "yes" evaluates to True or False.

Submission

Create a program called number_game.py that:

  1. Picks a "secret" number (just hardcode it, like secret = 7)
  2. Asks the user to guess the number
  3. Tells them if their guess is:
    • Too high
    • Too low
    • Correct!
  4. If correct, also tell them how far off they were (you can use abs() for absolute value)

Example interaction:

I'm thinking of a number between 1 and 10.
Your guess: 5
Too low! Try again.
Your guess: 8
Too high! Try again.
Your guess: 7
Correct! You got it!

Bonus: Add a message if they guess within 1 of the correct answer ("So close!").

Push your number_game.py to your GitHub repository.

Self-Check

Before submitting, verify:

Extension Challenges (Optional)

  1. Leap Year Checker: A year is a leap year if it's divisible by 4, except years divisible by 100 aren't leap years, unless they're also divisible by 400. Write a program that checks if a year is a leap year.

  2. Login System: Create a simple login that checks both username AND password. Give different error messages for "wrong username" vs "wrong password" vs "both wrong."

Getting Help