Practice and reinforce the concepts from Lesson 7
Time needed: 45-60 minutes
What you'll create: Three awesome programs using functions - a rectangle calculator, a bank account tracker, and a multiplication machine!
Check that you have:
Open EduBlocks and login with your Google account: https://app.edublocks.org/login
Create a new project:
Set up each exercise:
:bulb: :bulb: Pro tip: Save your work every 5-10 minutes! Press the Save button regularly to keep your progress safe.
Important: Keep all three exercise tabs open - you'll need the share links for submission!
Your Three Function Challenges
:emoji: Exercise One: Rectangle Area Calculator
Time: 15 minutes
Let's build a smart calculator that finds the area of any rectangle!
What you'll learn: How to create functions with parameters (inputs)
Steps to build:
- Drag the
def
function block from the Functions category
- Name your function:
area
- Add 2 parameters:
width
andheight
- Inside your function, create a variable:
- Drag a variable block and name it
result
- Set it to:
width * height
- Add a print statement inside the function:
- Print:
"Total area of rectangle: " + str(result)
- Outside the function, add input blocks:
- First input:
"Width: "
- Second input:
"Height: "
- Call your function:
- Drag the function call block
- Pass both input values to your
area
functiontip :emoji: Stuck? Remember that parameters are like empty boxes that get filled when you call the function. Yourwidth
andheight
parameters will receive the values from your inputs!
Time: 20 minutes
Create your own digital piggy bank that tracks money coming in and going out!
What you'll learn: How to use global variables in functions
Steps to build:
Create your starting balance:
balance
500
"Balance: " + str(balance)
Build the income function:
def
block and name it income
increment
global
block and type balance
+=
operator block to add increment to balance"Income: " + str(increment)
"Balance: " + str(balance)
Build the expense function:
def
block and name it expense
decrement
global
block and type balance
-=
operator block to subtract decrement from balance"Expense: " + str(decrement)
"Balance: " + str(balance)
Test your bank account:
income(300)
- You earned $300!expense(200)
- You spent $200income(150)
- More money coming in!expense(135)
- Another expense:bulb: :bulb: Why use global? The
global
keyword tells Python that you want to change thebalance
variable that exists outside the function. Without it, Python would create a new variable inside the function!
:emoji:️ Exercise 3: Multiplication Machine
Time: 20 minutes
Build a super-fast multiplication calculator that shows times tables!
What you'll learn: How functions can return values
Steps to build:
- Create the x2 function (multiply by 2):
- Drag a
def
block and name itx2
- Add one parameter:
n
- Inside: Add a
return
block withstr(n * 2)
- Create the x3 function (multiply by 3):
- Drag another
def
block and name itx3
- Add one parameter:
n
- Inside: Add a
return
block withstr(n * 3)
- Create the x4 function (multiply by 4):
- Drag another
def
block and name itx4
- Add one parameter:
n
- Inside: Add a
return
block withstr(n * 4)
- Get user input:
- Create a variable
n
- Set it to:
int(input("Number: "))
- Display the multiplication results:
- Print:
str(n) + " x 2 = " + x2(n)
- Print:
str(n) + " x 3 = " + x3(n)
- Print:
str(n) + " x 4 = " + x4(n)
tip :dart: Return vs Print: Thereturn
block sends a value back from the function, which you can then use in your print statements. It's like the function is answering a question!
Test your calculator with different numbers!
Common issues and fixes:
"Function not defined" error?
Global variable not changing?
global
keyword inside your function?global
block must be the first line inside the functionGetting weird output?
str()
to convert numbers to text+
signs connect text properlyInput not working?
int()
for numbersOnce you finish all three exercises, try these bonus challenges:
Rectangle Challenge: Modify Exercise 1 to also calculate the perimeter (distance around the rectangle). Formula: 2 * (width + height)
Bank Challenge: Add a new function called check_balance()
that just prints the current balance without changing it
Calculator Challenge: Create a new function x10
that multiplies by 10 and add it to your calculator
Super Challenge: Can you create a function that calls another function? Try making a double_income()
function that calls your income()
function twice!
After completing these exercises, you now know how to:
def
blockglobal
keywordReal Python Connection: In text-based Python, functions work exactly the same way! You're learning real programming concepts through visual blocks.
Ready to submit? Make sure you have:
Remember: You need to submit 3 project links - one for each exercise!
:bulb: Tip
:star2: Final tip: Functions are like recipes - they take ingredients (parameters), follow steps (function body), and sometimes give you back a result (return value). You're now a function chef!