Practice and reinforce the concepts from Lesson 8
Time needed: 30-40 minutes
What you'll create: Three awesome programs that use functions to:
In this activity, you will continue practicing Python functions with more advanced concepts. You'll learn to create functions that return values, work with mathematical operations, and handle conditional logic within functions.
:white_check_mark: Make sure you have:
Open EduBlocks and login with your Google account: https://app.edublocks.org/login
Start a new project:
Exercise (Extra Function) 1
:bulb: :bulb: Save Often!
Remember to save your work every 5-10 minutes! Just click the "Save" button at the top-right. This prevents losing your amazing code if something goes wrong.
Important: Keep all three exercise tabs open - you'll need the share links for submission!
Goal: Create a function that adds two numbers together and returns the result!
Step-by-Step Instructions:
def
block from the Functions categoryadd_num
and give it 2 parameters: num1
and num2
return
blocknum1 + num2
print
blockadd_num
function and enter the numbers 1 and 5:bulb: :emoji: What's "return"?
The
return
block tells your function to send back an answer! It's like when you ask a friend "What's 2+2?" and they return the answer "4" to you.
Goal: Create a smart function that tells you if a number is even or odd!
Step-by-Step Instructions:
def
block and name your function is_even
with 1 parameter: num
if/else
blocknum % 2 == 0
%
(modulo) operator from Mathnum
, %
, and 2
==
comparison blockif
section, drag a return
block with True
else
section, drag a return
block with False
print(is_even(9))
print(is_even(10))
:bulb: :dart: Understanding Modulo (%)
The
%
operator gives you the remainder after division. So10 % 2 = 0
(no remainder, it's even!) but9 % 2 = 1
(remainder of 1, it's odd!). Cool trick, right?
Your function should work like this:
Goal: Build a function that converts Fahrenheit to Celsius - super useful for science class!
Step-by-Step Instructions:
def
block and name your function f_to_c
with 1 parameter: temp
return
blocktemp - 32
5
9
(temp - 32) * 5 / 9
print(f_to_c(32))
- This should show 0°C (freezing point!)print(f_to_c(212))
- This should show 100°C (boiling point!):bulb: :emoji:️ Fun Temperature Facts!
- 32°F = 0°C (water freezes)
- 212°F = 100°C (water boils)
- 98.6°F = 37°C (normal body temperature) Try converting your own body temperature after completing the exercise!
The Magic Formula:
Celsius = (Fahrenheit - 32) × 5 ÷ 9
Function not working?
return
block is INSIDE the function (indented)Getting weird results?
%
(modulo) not /
(division)(temp - 32)
Can't see output?
print()
blocks around your function callsYour completed exercises must include:
Want to level up? Try these bonus challenges:
:bulb: :star2: Pro Tip!
Functions with return values are like building blocks in real Python programming. Game developers use them to calculate scores, scientists use them for formulas, and app developers use them to process user data. You're learning real programming skills!
When you have completed your "Python Extra Function Exercises" activity, submit it using the link below: Submit Your Project Here
Before submitting, check:
Note: You need to submit 3 project links for this activity - one for each exercise.
Great job learning about functions that return values! These are super powerful tools in programming! :tada: