Karel, Part 2
Neel Kishnani, CS106A, Summer 2025
Kishnani, CS106A, Summer 2025
Recap
Kishnani, CS106A, Summer 2025
Anatomy of a Program
myCodeFile.py
Magic Start Function
Main Function
Your program starts here!
Helper Functions
Custom functions that are called from your main function
Imports
Code that others wrote that you use in your function
Kishnani, CS106A, Summer 2025
Define Functions Before Using Them
Analogy: Prep your vegetables before cooking
🧑🍳🍳
Kishnani, CS106A, Summer 2025
for loops
🔁
Kishnani, CS106A, Summer 2025
for i in range(num_iterations):
# Code to repeat goes here
Indent
This colon is required when writing a for loop
Kishnani, CS106A, Summer 2025
while loops
🔁
Kishnani, CS106A, Summer 2025
while condition:
# Code to repeat goes here
Indent
This colon is required when writing a while loop
Kishnani, CS106A, Summer 2025
Conditions
Kishnani, CS106A, Summer 2025
Condition | Opposite | Notes |
front_is_clear() | front_is_blocked() | Is there a wall in front of Karel? |
left_is_clear() | left_is_blocked() | Is there a wall to the left of Karel? |
right_is_clear() | right_is_blocked() | Is there a wall to the right of Karel? |
beepers_present() | no_beepers_present() | Are there beepers for Karel on this cell? |
beepers_in_bag() | no_beepers_in_bag() | Are there beepers for Karel to put down? |
facing_north() | not_facing_north() | Is Karel facing north? |
facing_south() | not_facing_south() | Is Karel facing south? |
facing_east() | not_facing_east() | Is Karel facing east? |
facing_west() | not_facing_west() | Is Karel facing west? |
Kishnani, CS106A, Summer 2025
Which Loop Should I Use?
Need to repeat lines of code
for loop
while loop
Don’t know how many repetitions(indefinite)
Know how many repetitions (definite)
Kishnani, CS106A, Summer 2025
if statements
Kishnani, CS106A, Summer 2025
if statement syntax in Python (2 ways)
if condition:
# Code to execute if condition is true
This block of code works as-is!
Indent
This colon is required when writing an if statement
Same conditions as the while loop!
(i.e. front_is_blocked())
Kishnani, CS106A, Summer 2025
if statement syntax in Python (2 ways)
if condition:
# Code to execute if condition is true
else:
# Code to execute if condition is false
Indent
Indent
Kishnani, CS106A, Summer 2025
Kishnani, CS106A, Summer 2025
Let’s Practice: Diagonal Karel
Kishnani, CS106A, Summer 2025
Preconditions and Postconditions
def move_to_wall():
"""
Precondition: None
Postcondition: Karel is facing a wall at the end of a row/column
"""
while front_is_clear():
move()
Kishnani, CS106A, Summer 2025
End Recap
Kishnani, CS106A, Summer 2025
Decomposition
Kishnani, CS106A, Summer 2025
Decomposition
Kishnani, CS106A, Summer 2025
Decomposition
Kishnani, CS106A, Summer 2025
Decomposition
def make_cookies():
Kishnani, CS106A, Summer 2025
Decomposition
def make_cookies():
make_dough()
form_cookies()
bake_cookies()
Kishnani, CS106A, Summer 2025
Decomposition
def bake_cookies():
def form_cookies():
def make_dough():
def make_cookies():
make_dough()
form_cookies()
bake_cookies()
Kishnani, CS106A, Summer 2025
Decomposition
def bake_cookies():
def form_cookies():
def make_dough():
def make_cookies():
make_dough()
form_cookies()
bake_cookies()
This could break down into even more subtasks!
Kishnani, CS106A, Summer 2025
Decomposition
def combine_wet_ingredients()
def combine_dry_ingredients()
def make_dough():
combine_dry_ingredients()
combine_wet_ingredients()
def make_cookies():
make_dough()
form_cookies()
bake_cookies()
Small, manageable tasks make the process easier!
Kishnani, CS106A, Summer 2025
Mountain Climber Karel
Kishnani, CS106A, Summer 2025
Mountain Climber Karel
Live coding!
Kishnani, CS106A, Summer 2025
Mountain Climber Karel
def descend_mountain():
while front_is_clear():
step_down()
def climb_mountain():
while front_is_blocked():
step_up()
def main():
climb_mountain()
put_beeper()
descend_mountain()
Kishnani, CS106A, Summer 2025
Kishnani, CS106A, Summer 2025
Kishnani, CS106A, Summer 2025
Style Guidance
⭐️ Use these tips! Your future self will thank you! ⭐️
Kishnani, CS106A, Summer 2025
Questions?
Kishnani, CS106A, Summer 2025
Housekeeping
Kishnani, CS106A, Summer 2025
Common Errors
Infinite Loop
Kishnani, CS106A, Summer 2025
Infinite Loop
Kishnani, CS106A, Summer 2025
Infinite Loop
while front_is_clear():
turn_left()
🪲🪲🪲🪲
Kishnani, CS106A, Summer 2025
Infinite Loop
while front_is_clear():
turn_left()
Kishnani, CS106A, Summer 2025
Task: Roomba Karel (pick up all beepers)
Kishnani, CS106A, Summer 2025
Task: Roomba Karel (pick up all beepers)
Kishnani, CS106A, Summer 2025
Possible Algorithm
Kishnani, CS106A, Summer 2025
Possible Algorithm
Kishnani, CS106A, Summer 2025
Possible Algorithm
Kishnani, CS106A, Summer 2025
Possible Algorithm
Kishnani, CS106A, Summer 2025
Kishnani, CS106A, Summer 2025
Possible Algorithm ✅
Kishnani, CS106A, Summer 2025
Live Coding!