Python Functions ๐ | 1.3 Karel Can't Turn Right
๐
PythonCodeHS 1.3Nearpod Lesson
Python Functions Your Code Superpowers Unlocked!
Today YOU become a Python Software Engineer. Build real functions, solve Karel problems, and write code like the pros at Google and Netflix.
๐ฏ What Are Functions?
Think of a Function as Your Personal Robot Assistant
You teach it how to do a specific task once, and then you can ask it to do that task whenever you want โ just by saying its name!
Python
1
defturn_right():
2
turn_left()
3
turn_left()
4
turn_left()
5
6
# Now you can use it!
7
turn_right()# Karel turns right!
๐ Python Function Writing Format โ Line by Line:
Line 1: Start with the def keyword
Line 1: Add your function name using lowercase_with_underscores
Line 1: Add parentheses () after the name
Line 1: End with a colon : โ never forget this!
Lines 2+: Indent 4 spaces (or 1 tab) for every command in the body
Body: Write all commands that should run when you call the function
๐ Real-World Example
Making Pizza
Would you really type all these steps every single time someone orders pizza? What if there are 1,000 orders?
โ Without Functions
Every time you want pizza, you repeat:
Roll dough
Add sauce
Add cheese
Add toppings
Bake for 15 minutes
...repeated 1,000ร ๐ญ
โ
โ With a Function
Create make_pizza() once, then call it!
Python
1
defmake_pizza():
2
roll_dough()
3
add_sauce()
4
add_cheese()
5
bake(15)
6
7
# 1,000 orders = 1,000 calls:
8
make_pizza()โ
๐ ๏ธ Function Anatomy
Dissecting a Python Function โ Line by Line
Every part has a job. Learn each one and you'll never write a broken function again.
Python
1
deffunction_name():โ Function definition
2
# Function body โ These are the steps
3
command1()
4
command2()
5
command3()
6
7
function_name()โ Calling the function
๐
def
Stands for "define." Tells Python you are creating a new function.
๐
snake_case Name
Lowercase with underscores. Describes exactly what the function does.
๐ต
Colon (:)
Required at the end of line 1. If you forget it, Python throws an error!
โก๏ธ
Indentation
4 spaces or 1 tab. Python requires this to know what's inside the function.
๐ฑ Real-World: Your Morning Routine
One Call. Five Tasks Done.
Instead of doing 5 things every morning manually, you just call ONE function!
Python
1
defget_ready_for_school():
2
wake_up()
3
brush_teeth()
4
get_dressed()
5
eat_breakfast()
6
pack_backpack()
7
8
# Every morning you just run:
9
get_ready_for_school()
โก Power of Functions: Instead of calling 5 separate commands every morning, you just call get_ready_for_school() once โ and all 5 steps run automatically!
โ๏ธ Guided Practice โ 5-Step Engineering Process
Solving the Karel Problem
Real engineers follow a process. You will too โ right now!
1
Identify the Problem
Karel can't turn right. We need this capability to solve more complex mazes and towers!
2
Design a Solution
How can we simulate a right turn using only turn_left()? Think about a compass โ 3 left turns = 1 right turn!
3
Implement โ Write the Smart Shortcut
Python
1
defturn_right():
2
turn_left()
3
turn_left()
4
turn_left()
4
Test the Solution ๐
Stand up! Turn left three times together. Did you end up facing the same direction as one right turn? Yes โ it works! Our engineering solution is verified.
5
Use It Efficiently
Apply turn_right() in CodeHS 1.3.3: Tower and Turn Right. One call replaces three turn_left() commands every time!
๐ช Why Functions Are Awesome
4 Superpowers Every Engineer Uses
This is why professional engineers at Google, Netflix, and your favorite game studios love functions!
๐
No More Repetition
Write once, use everywhere! This is the DRY Principle โ Don't Repeat Yourself.
๐งน
Cleaner Code
Your code looks organized and professional โ like a tidy room vs. a messy one!
๐
Easier Debugging
If something breaks, you only fix it in one place โ not 100 times!
๐ค
Better Teamwork
Other programmers can easily understand and use your functions without knowing how they work inside.
๐ Cool Real-World Function Examples
Functions Power Everything You Use
Every time you respawn, post a photo, start a movie, or ride in a self-driving car โ a function is running.
๐ฎ Gaming
Python
1
defrespawn_player():
2
restore_health(100)
3
move_to_checkpoint()
4
reset_inventory()
5
play_respawn_sound()
When called: all 4 commands run automatically!
๐ฒ Social Media
Python
1
defpost_photo():
2
take_picture()
3
apply_filter()
4
add_caption()
5
tag_friends()
6
share_to_feed()
Just one call does all 5 posting steps!
๐ Smart Home
Python
1
defmovie_night():
2
dim_lights()
3
turn_on_tv()
4
set_volume(15)
5
close_curtains()
6
order_popcorn()
One command sets up your whole movie experience!
๐ Self-Driving Car
Python
1
defpark_car():
2
check_spot()
3
signal_turn()
4
slow_down()
5
turn_wheel()
6
reverse_slowly()
7
stop()
Complex parking broken into simple steps!
๐ช Interactive Function Demo
Watch a Function Execute Live!
Click the button to see make_sandwich() run step by step.
Python
1
defmake_sandwich():
2
get_bread()
3
add_peanut_butter()
4
add_jelly()
5
put_top_slice()
6
cut_diagonally()
7
8
make_sandwich()โ Click Run!
Output will appear here when you click Run...
๐ป CodeHS Activities
Apply Your Superpowers!
Open CodeHS and complete both challenges using your new turn_right() function.
๐ผ
Activity 4 โ 1.3.3: Tower and Turn Right Individual
Apply turn_right() to build a tower and navigate Karel. Check: correct def, colon, and indentation!