1 of 47

Karel, Part 2

Neel Kishnani, CS106A, Summer 2025

2 of 47

Kishnani, CS106A, Summer 2025

Recap

3 of 47

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

4 of 47

Kishnani, CS106A, Summer 2025

Define Functions Before Using Them

Analogy: Prep your vegetables before cooking

🧑‍🍳🍳

5 of 47

Kishnani, CS106A, Summer 2025

for loops

  • Allows you to execute lines of code N times (where you decide N)
  • In order to use a for loop you must:
    • Know the number N
    • Know what lines of code you want to repeat

🔁

6 of 47

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

7 of 47

Kishnani, CS106A, Summer 2025

while loops

  • Allows you to execute lines of code while some condition is true
  • In order to use a while loop you must:
    • Know the condition to check
    • Know what lines of code you want to repeat

🔁

8 of 47

Kishnani, CS106A, Summer 2025

while condition:

# Code to repeat goes here

Indent

This colon is required when writing a while loop

9 of 47

Kishnani, CS106A, Summer 2025

Conditions

  • Statement that is either true or false
  • For Karel: some state of the world (i.e. is there a beeper on current cell? is there a wall in front of me?)
  • This condition is checked on every iteration of the loop
    • If the statement is true, execute the body of the loop
    • If the statement is false, move on to the next line of code following the loop

10 of 47

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?

11 of 47

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)

12 of 47

Kishnani, CS106A, Summer 2025

if statements

  • Allows you to execute lines of code if some condition is true
  • In order to use a if statement you must:
    • Know the condition to check
    • Know what lines of code you want to execute if the condition is true

13 of 47

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())

14 of 47

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

15 of 47

Kishnani, CS106A, Summer 2025

16 of 47

Kishnani, CS106A, Summer 2025

Let’s Practice: Diagonal Karel

17 of 47

Kishnani, CS106A, Summer 2025

Preconditions and Postconditions

  • Helpful to write out the “state of Karel’s world” before and after a function is called
  • Especially useful for helper functions

def move_to_wall():

"""

Precondition: None

Postcondition: Karel is facing a wall at the end of a row/column

"""

while front_is_clear():

move()

18 of 47

Kishnani, CS106A, Summer 2025

End Recap

19 of 47

Kishnani, CS106A, Summer 2025

Decomposition

  • Breaking down large problems into manageable subtasks
  • Great for readability, simplicity, etc.
  • Key in the Art of Problem Solving

20 of 47

Kishnani, CS106A, Summer 2025

Decomposition

  • Making cookies is hard
  • Break down into smaller steps
    • Make dough
    • Form cookies
    • Bake

21 of 47

Kishnani, CS106A, Summer 2025

Decomposition

22 of 47

Kishnani, CS106A, Summer 2025

Decomposition

def make_cookies():

23 of 47

Kishnani, CS106A, Summer 2025

Decomposition

def make_cookies():

make_dough()

form_cookies()

bake_cookies()

24 of 47

Kishnani, CS106A, Summer 2025

Decomposition

def bake_cookies():

def form_cookies():

def make_dough():

def make_cookies():

make_dough()

form_cookies()

bake_cookies()

25 of 47

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!

26 of 47

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!

27 of 47

Kishnani, CS106A, Summer 2025

Mountain Climber Karel

28 of 47

Kishnani, CS106A, Summer 2025

Mountain Climber Karel

Live coding!

29 of 47

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()

30 of 47

Kishnani, CS106A, Summer 2025

31 of 47

Kishnani, CS106A, Summer 2025

32 of 47

Kishnani, CS106A, Summer 2025

Style Guidance

  • Functions should do “one conceptual thing”
  • The function name should clearly show what it does
  • For this class: less than 10 lines, 3 levels of indentation
  • Reusable and easy to modify
  • Well commented

⭐️ Use these tips! Your future self will thank you! ⭐️

33 of 47

Kishnani, CS106A, Summer 2025

Questions?

34 of 47

Kishnani, CS106A, Summer 2025

Housekeeping

  • Assignment 1 (Karel the Robot) is out now!
    • Due back on Friday July 4th at 11:59PM
  • LaIR opens Sunday, runs Sun-Thu 5-9PM in CoDa E160
  • Office Hours:
    • Neel: M/F 1-2PM in CoDa B45
    • Iddah: Tu/Th 2-4PM in CoDa B45
  • Section starts next week!

35 of 47

Kishnani, CS106A, Summer 2025

Common Errors

Infinite Loop

36 of 47

Kishnani, CS106A, Summer 2025

Infinite Loop

  • If you actually follow these instructions, you’ll never exit the shower!
  • Cautionary tale: Make sure your while loops can actually exit

37 of 47

Kishnani, CS106A, Summer 2025

Infinite Loop

while front_is_clear():

turn_left()

38 of 47

🪲🪲🪲🪲

Kishnani, CS106A, Summer 2025

Infinite Loop

while front_is_clear():

turn_left()

39 of 47

Kishnani, CS106A, Summer 2025

Task: Roomba Karel (pick up all beepers)

40 of 47

Kishnani, CS106A, Summer 2025

Task: Roomba Karel (pick up all beepers)

  • Karel starts at (1, 1)
  • There are no interior walls
  • There are beepers in the world, scattered everywhere
  • Karel must pick up all beepers
  • Karel can end anywhere

41 of 47

Kishnani, CS106A, Summer 2025

Possible Algorithm

42 of 47

Kishnani, CS106A, Summer 2025

Possible Algorithm

43 of 47

Kishnani, CS106A, Summer 2025

Possible Algorithm

44 of 47

Kishnani, CS106A, Summer 2025

Possible Algorithm

45 of 47

Kishnani, CS106A, Summer 2025

46 of 47

Kishnani, CS106A, Summer 2025

Possible Algorithm ✅

47 of 47

Kishnani, CS106A, Summer 2025

Live Coding!