🐍 Python for kids · Class 5 · Lesson 15 of 18 · Free
Problem Solving with Functions
⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Break a big problem into small steps, one function for each step.
- Pass the answer of one function into another function.
- Build a menu program that repeats until the user chooses to exit.
How do you eat a whole jackfruit? Not in one bite! You cut it into small pieces first. Programmers solve big problems the same way: they cut them into small functions.
Why break a big problem into small functions?
A long program written in one piece is hard to read. When something goes wrong, it is hard to find the mistake.
Small functions each do one job. You can test each job alone, and fix it without touching the rest.
Good function names also explain the program. Read the last three lines below: they tell the whole story.
▶def show_title():2 print("=== Sports Day Entry ===")34def show_entry(name, event):5 print(f"{name} will run the {event}.")67show_title()8show_entry("Tanvi", "100 m race")9show_entry("Om", "relay")
No variables yet.
▌✏️ Change this code and run it yourself
=== Sports Day Entry === Tanvi will run the 100 m race. Om will run the relay.
How do I plan functions before writing code?
First, write the problem in plain words. Then list the small steps, like a recipe.
Problem: make a report for one student. Steps: add the marks, find the grade, print the report.
Each step becomes one function. Most programs follow the same pattern: input, process, output.
Plan ready. Now write one function per step!
💡 Lines starting with # are comments. Python skips them, but they help you think.
How do functions work together?
Now we write two functions from our plan. Watch how the answer of one function becomes the input of the next.
get_total returns a number. We store it in total, then pass total to get_grade.
Each function is short, so each one is easy to check.
▶def get_total(marks):2 return sum(marks)34def get_grade(total):5 if total >= 240:6 return "A"7 return "B"89total = get_total([85, 78, 90])10print("Diya:", total, "marks, grade", get_grade(total))
No variables yet.
▌✏️ Change this code and run it yourself
Diya: 253 marks, grade A
Can one function call another function?
Yes. A function can use another function inside it, just like you use print() inside your functions.
Here, canteen_bill calls price_of for each item. price_of looks up the price in a dictionary.
If the prices change, you only fix the dictionary. canteen_bill does not change at all.
Bill: 40 rupees
How do I build a menu program with functions?
Many real apps show a menu, like an ATM or a ticket machine. The user picks a number, and the app does that job.
We use a while True loop to show the menu again and again. Each choice calls its own function. Choice 3 uses break to leave the loop.
The inputs below are already typed for you: first 1, then 2, then 3.
1) Add 12 and 8 2) Multiply 12 and 8 3) Exit Your choice: 1 Answer: 20 1) Add 12 and 8 2) Multiply 12 and 8 3) Exit Your choice: 2 Answer: 96 1) Add 12 and 8 2) Multiply 12 and 8 3) Exit Your choice: 3 Bye!
How do I test each function on its own?
Here is get_grade with a third grade, C. Before joining functions together, test each one with a few easy values. You already know the right answers, so mistakes show up quickly.
Try the edge values too, like exactly 240 or exactly 180. Bugs love to hide at the edges.
A should be A A should be A B should be B C should be C
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
def double(n):
return n * 2
def add_one(n):
return n + 1
print(add_one(double(5)))Question 2. What does this print?
def is_pass(marks):
return marks >= 33
def result(marks):
if is_pass(marks):
return "Pass"
return "Fail"
print(result(33))Question 3. What does this print?
def square(n):
return n * n
total = 0
for x in [1, 2, 3]:
total = total + square(x)
print(total)🧩 Your challenge
Build a small Temperature Helper menu. Option 1 converts 37 degrees Celsius to Fahrenheit, option 2 says if 37 degrees is a fever, option 3 exits. Write one function for each job. The inputs 1, 2 and 3 are typed for you.
1) Convert 2) Fever check 3) Exit: 1 37 C is 98.6 F 1) Convert 2) Fever check 3) Exit: 2 No fever. You are fine. 1) Convert 2) Fever check 3) Exit: 3 Goodbye!
⚠️ Common mistakes (and how to fix them)
❌ Wrong
def get_total(marks):
total = sum(marks)
result = get_total([80, 90])
print("Total:", result)No error — but it prints Total: None, because the function never returns its answer.
✅ Right
def get_total(marks):
total = sum(marks)
return total
result = get_total([80, 90])
print("Total:", result)A function without return gives back None. Add return to send the answer out.
❌ Wrong
print(get_grade(250))
def get_grade(total):
if total >= 240:
return "A"
return "B"NameError: name 'get_grade' is not defined
✅ Right
def get_grade(total):
if total >= 240:
return "A"
return "B"
print(get_grade(250))Python reads from top to bottom, so define a function before you call it.
❌ Wrong
while True:
choice = input("1) Hello 2) Exit: ")
if choice == "1":
print("Hello!")
elif choice == 2:
print("Bye!")
breakNo error message — the program never stops (press Stop).
✅ Right
while True:
choice = input("1) Hello 2) Exit: ")
if choice == "1":
print("Hello!")
elif choice == "2":
print("Bye!")
breakinput() gives text, so compare with "2" in quotes, not the number 2.
📌 Remember
- Break a big problem into small steps, and write one function per step.
- Plan first in plain words: input, process, output.
- The returned answer of one function can be passed into another function.
- A function can call another function inside it.
- A menu program uses while True, calls a function for each choice, and break to exit.
- Test each function alone with values whose answers you already know.
Finished the quiz and the challenge?
Questions students ask
How small should a function be?
A good function does one clear job. If you need the word "and" to describe it, it may be better as two functions.
Why does my function print None?
The function has no return line, so Python gives back None. Add return with the value you want to send out.
What does while True mean?
It is a loop whose condition is always True, so it repeats forever. You stop it from inside with break, usually when the user chooses Exit.
In what order should I write my functions?
Write all the def blocks at the top. Put the main code that calls them at the bottom, so every function is ready before it is used.