3.03 Math Expressions

Source lesson: csp/python/mathexpressions/lesson

Learning Objective: Write math expressions with Python operators so the program calculates outputs from variables instead of hardcoded numbers.

Popcorn Hack 1: Try the Basics

Code Runner Challenge

Popcorn Hack - Basic Arithmetic Operators

View IPYNB Source
# CODE_RUNNER: Popcorn Hack - Basic Arithmetic Operators
value_a = 100
value_b = 10
value_c = 25

addition = value_a + value_b + value_c
subtraction = value_a - value_b
multiplication = value_b * value_c
division = value_a / value_b

print("Total:", addition)
print("Difference:", subtraction)
print("Product:", multiplication)
print("Division:", division)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Poway Recovery Center Booking

Code Runner Challenge

Python Version of Popcorn Hack 2

View IPYNB Source
# CODE_RUNNER: Python Version of Popcorn Hack 2
total_monthly_capacity = 100
spots_requested_per_group = 4
sessions_booked = 3

total_spots_taken = spots_requested_per_group * sessions_booked
total_remaining_capacity = total_monthly_capacity - total_spots_taken
users_per_room = total_remaining_capacity / 2  # 2 active rooms

print(f"Total Spots Taken: {total_spots_taken}")
print(f"Remaining Capacity: {total_remaining_capacity}")
print(f"Users per Room: {users_per_room}")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework: Recovery Center Cost Report

Code Runner Challenge

Recovery Center Cost Report

View IPYNB Source
# CODE_RUNNER: Recovery Center Cost Report
cost_per_session = 35
sessions_per_week = 3
weeks_per_month = 4
enrollment_fee = 75

weekly_cost = cost_per_session * sessions_per_week
monthly_cost = weekly_cost * weeks_per_month
total_cost_with_fee = monthly_cost + enrollment_fee
average_daily_cost = monthly_cost / 30

print("Weekly Cost: ", weekly_cost)
print("Monthly Cost: ", monthly_cost)
print("Total Cost with Fee: ", total_cost_with_fee)
print("Average Daily Cost: ", average_daily_cost)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...