Practice and reinforce the concepts from Lesson 12
Estimated Time: 45-60 minutes
What You'll Create: Three awesome mathematical calculators using Python's math module!
:white_check_mark: Make sure you're logged into EduBlocks
:white_check_mark: Have a calculator handy for checking your work
:white_check_mark: Remember to save your work every 5-10 minutes
:white_check_mark: You'll be creating 3 separate projects today
Go to EduBlocks and login:
https://app.edublocks.org/login
For each new exercise:
:bulb: Save Your Progress!
Set a timer for every 10 minutes to remind yourself to save. This prevents losing your hard work!
The Challenge: You're working at a food shop during a special 30% OFF sale! Your job is to quickly calculate the new prices for all menu items.
Step-by-Step Instructions:
Import the math module by dragging the import math
block to your workspace
Create an input block to ask for the original price:
input()
blockprice
Calculate the discount:
discount_amount
price * 0.3
(that's 30%!)Find the new price:
new_price
price - discount_amount
Round up using math.ceil():
math.ceil()
blocknew_price
inside itDisplay the result:
print()
block to show the final price:bulb: Why Round Up?
In real stores, prices like $3.21 are often rounded to $3.25 or $4.00 to make giving change easier. The
math.ceil()
function always rounds UP to the next whole number!
The Challenge: Create a calculator that finds the longest side (hypotenuse) of a right triangle! This is super useful in games for calculating distances.
Understanding the Pythagorean Theorem:
The formula is: c² = a² + b² (where c is the hypotenuse)
Step-by-Step Instructions:
Import the math module (if not already imported)
Get the first side length:
input()
blocka
Get the second side length:
input()
blockb
Square both sides using math.pow():
a_squared
= math.pow(a, 2)
b_squared
= math.pow(b, 2)
Add the squares together:
c_squared
= a_squared + b_squared
Find the square root:
math.sqrt(c_squared)
to get the hypotenusehypotenuse
Display the result:
:bulb: Real-World Connection
The Pythagorean theorem is used in video games to calculate the distance between characters, in construction to ensure buildings are square, and even in GPS navigation!
The Challenge: You're a ninja dodging a rotating turret! Calculate how many degrees the turret needs to turn to track your movement. This uses the same math that video games use for enemy AI!
The Scenario:
The Math Formula:
Step-by-Step Instructions:
Import math module (if needed)
Get the distance the ninja ran:
input()
blockdistance_run
Set the radius (distance from turret):
radius
= 250 (this is constant)Calculate the angle in radians:
angle_radians
= distance_run / radius
Convert radians to degrees:
angle_degrees = angle_radians * (180 / math.pi)
Display the result:
:bulb: Understanding Radians vs Degrees
- Radians are how mathematicians measure angles
- Degrees are what we use in everyday life (360° in a circle)
- Python's math functions use radians, so we often need to convert!
Test Your Calculator:
Common Issues and Solutions:
"NameError: name 'math' is not defined"
import math
at the very top of your code!Getting decimal numbers in the discount calculator?
math.ceil()
to round upPythagorean calculator giving wrong answers?
math.pow(a, 2)
not math.pow(2, a)
Turret calculator showing huge numbers?
degrees = radians * (180 / math.pi)
Once you've completed all three exercises, try these bonus challenges:
Discount Calculator Plus:
Triangle Calculator Deluxe:
Advanced Turret Tracker:
The blocks you're using create real Python code:
import math
-> Gives you access to advanced math functionsmath.ceil()
-> Always rounds UP (ceiling)math.sqrt()
-> Square root functionmath.pow()
-> Raises numbers to any powermath.pi
-> The value of π (3.14159...)Make sure each exercise has:
When you have completed all three "Python Math Module Exercises", submit them using the link below:
Important: You need to submit 3 separate project links - one for each exercise. Make sure to:
Great job exploring the power of Python's math module! :tada: