3.03 Math Expressions
Learn about math commands and how it connects to real math problems as well as how to implicate it into games.
Math Expressions Lesson
Part 1 β The Basics: What is a Math Expression?
Why this matters: Every program that calculates a price, a score, a distance, or a time is built on math expressions. A math expression combines numbers and operators so the computer evaluates them into a single new value β without expressions, a program can only store numbers, it can never actually do anything with them.
Python uses the same basic operators as a calculator:
| Operator | Meaning | Description | Usage Example |
|---|---|---|---|
+ |
Addition | Adds two values together. | 10 + 5 evaluates to 15 |
- |
Subtraction | Subtracts the second value from the first. | 10 - 5 evaluates to 5 |
* |
Multiplication | Multiplies two values. | 10 * 5 evaluates to 50 |
/ |
Division | Divides the first value by the second. | 10 / 5 evaluates to 2.0 |
** |
Exponentiation | Raises the first value to the power of the second. | 2 ** 3 evaluates to 8 |
// |
Floor Division | Divides and rounds down to the nearest whole number. | 10 // 3 evaluates to 3 |
% |
Modulus | Divides and returns only the remainder. | 10 % 3 evaluates to 1 |
Addition
Example: Addition
num1 = 10 num2 = 5 print(β10 + 5 evaluates to:β, num1 + num2) # Evaluates to 15# Challenge: Fix the type error or syntax so this runs successfully!
Hint: You cannot directly add a string and an integer in Python.
total = 10 + β5β print(total)
Subtraction
Example: Subtraction
num1 = 10 num2 = 5 print(β10 - 5 evaluates to:β, num1 - num2) # Evaluates to 5# Challenge: Find and fix the variable name typo in this code! result = 10 - 5 print(resul)
Multiplication
Example: Multiplication
num1 = 10 num2 = 5 print(β10 * 5 evaluates to:β, num1 * num2) # Evaluates to 50# Challenge: Fix the invalid operator used for multiplication in Python! product = 10 x 5 print(product)
Division
Example: Division
num1 = 10 num2 = 5 print(β10 / 5 evaluates to:β, num1 / num2) # Evaluates to 2.0[cite: 1]# Challenge: Fix the ZeroDivisionError by changing the divisor! quotient = 10 / 0 print(quotient)
Modulus
Example: Modulus
num1 = 10 num2 = 3 print(β10 % 3 evaluates to:β, num1 % num2) # Evaluates to 1[cite: 1]# Challenge: Complete the code using the modulus operator to find the remainder of 10 divided by 3! remainder = 10 print(remainder) β
π Homework-Sriansh
(3 Minutes): Recovery Center Cost Report
In the same portfolio notebook, add a new code cell below your interactive task and build a full cost report.
As you can see there is a system that uses divisibility to determine every 5 levels of a game. When any number has a % and then a number after that means that everytime that number can be divided by the other number to trigger a command. Try putting in different values that 5 goes into. See if it triggers a boss level. Now try without a number that 5 goes into. See if it triggers a different message.
Commit and push your notebook when youβre done to save your work and output β youβll link it in the submission form below.
# Game Boss Level Detector Challenge
player_level = 15
# Use the modulo operator (%) to check if player_level is divisible by 5
is_boss_level = player_level % 5
if is_boss_level == 0:
print("β οΈ WARNING: Level " + str(player_level) + " is a Boss Level! Prepare")
else:
print("Level " + str(player_level) + " cleared! Regular enemies ahead.")
π Homework-Savithr (3 Minutes): Recovery Center Cost Report
In the same portfolio notebook, make or add a new code cell below your interactive task and build a full cost report.
In this new assignment. I want you to test some basic math equations. As you can see I have sey values for the base score their multiplier and some bonus points. The main goal of this assignment it to create your own unique way of multiplying and adding scores based of different power ups or games. Try using the variables and their values and using different operations to change the score value so that when you output the value after all the opperations it generates ac accurate score. You can do the math in your head or use a calculator to check. Then for a more complicated challenge try seeing that if your points are divisible by 25 then you get an extra life but if you dont then it tells you how many more points you need for an extra life. This is a way of using % sign in order to make it so you need a certain amount of points (25) in order to buy an extra life.
Commit and push your notebook to github when youβre done to save your work and output, you will link it in the submission form below.
# Game Score & Bonus Tracker
base_score = 50
multiplier = 2
bonus_points = 15
# Calculate total score using PEMDAS
total_score = base_score * multiplier + bonus_points
print("Total Score: " + str(total_score))
# Modulo check: Award extra life every 25 points
remainder = total_score % 25
if remainder == 0:
print("Bonus! You earned an extra life!")
else:
print("Points needed for next extra life: " + str(25 - remainder))
Submit Assignment
Need to update a submission later? Open the submissions dashboard.