Karel 3

Calvin (Deutschbein)

W1D3: 30 Aug


Announcements

  • Welcome to CS-151: Intro to Programming with Python!
  • By FRIDAY 11:59 PM: Fill out this survey
    • There are weekly, 1 hour meetings with peer tutors on Wednesday or Thursday.
    • Attending these is 10% of your grade.
    • Read the instructions carefully and fill out as many times as you can!
  • By MONDAY 11:59 PM: "Introductions" assignment. 31 done + 8 start out of 48
    • A minimal problem set to familiarize yourself with how to submit coding assignments.
    • Links will be posted on the course website and on the Discord.
      • They will not be posted on Canvas or via email to help encourage you to use the website and Discord
  • Other stuff
    • Visit the webpage @ cd-public.github.io/courses/cs1f24
    • Join the Discord server - you got an email invite from me ("ckdeutschbein@")
    • Bring a computer if at all possible, or let me know to find you one.

Today

  • "Control Flow"
  • Avoiding repeating ourselves, as much as possible.
    • The "def" (for "define") keyword and functions
    • The "for" keyword and loops
    • The "if" keyword and conditionals
    • The "while" keyword and conditional loops

Functions

  • Rather than turn left three times, we can make a turn right "function"

Define

Use

Format:

  • 'def' keyword, followed by
  • a function name, like "turn_right"
  • parens "()" and a colon ':'
  • a newline ("enter") and indent ("tab")
  • some 'code block' like 3 turns

Example:

def turn_right(): turn_left() turn_left() turn_left()

Format:

  • like "move()" or "turn_left()"
  • write the function name, like "turn_right"
  • follow with open and close parens "()"
  • that's it!

    Example:

    def main(): turn_right() move() turn_right()

    Nascar no more!

Potholes

Potholes

  • We can imagine potholes as being as kind of scooped out spaces at the bottom/south of our grid.
  • We can write a "fill pothole" function to place a beeper (tar) in a pothole (space).
  • Ask me about the word "function" and my municipal government... but not during class!

Potholes ⇒ Notholes <slaps knee>

def fill_pothole(): turn_right() move() put_beeper() turn_left() turn_left() move() turn_right() def main(): move() fill_pothole() move() move() fill_pothole() move()

Sidebar: Comments

Or "What does "fill_pothole" mean?"

  • It is always a good idea to document and leave notes to yourself
  • Comments are pieces of text in your program that are ignored when the program is run
  • Python comments are denoted with a leading hashtag:
    • Everything following a hashtag (#) on the same line is ignored

      def fill_pothole(): # Places a beeper to Karel's right and returns
    • "Until you write code that works the first time, you should comment every line." - me
  • Triple quote method: we can include multiple lines of text inside triple quotes.

    def fill_pothole(): """ Two lines"""
    • This isn't a true comment, but we use it to give instructions on assignments.
    • My advice: Use #

Potholes ⇒ Notholes <slaps knee>

    def fill_pothole(): turn_right() # face pothole move() # go to pothole put_beeper() # place "tar" turn_left() # reverse 1/2 turn_left() # reverse 2/2 move() # exit pothole turn_right() # return to original direction
  • If you don't type this much, at least think this much.
  • If I ask you what a line of code does, be ready to answer.

Today

  • "def" (for "define") keyword and functions
  • The "for" keyword and loops
  • The "if" keyword and conditionals
  • The "while" keyword and conditional loops

Loops

  • I don't like typing something more than once.
  • Example:

    def turn_right(): turn_left() # turn right 1/3 turn_left() # turn right 2/3 turn_left() # turn right 3/3
  • If I want to do something some known number of times, I can use a loop
  • My favorite kind of loop is a "for" loop, with the "for" keyword
  • Example:

    def turn_right(): for i in range(3): # do something 3 times turn_left() # the thing to do 3 times
  • 'i' stands 'iterations', and is a historical name like 'main'
  • But unlike main, it can be changed to any other name (within reason)

Exercise

  • Write "move_five" and "turn_around" with "for" loops.
  • Use comments
    • (It doesn't matter what you think "move_five" means, just that you can write code that does what you say it will)
  • Reminder: for j in range(n): # statements to be repeated
    • "n" is the number of times to repeat
    • 'j' stands for 'jterations' (you don't have to use 'i')

Today

  • "def" (for "define") keyword and functions
  • "for" keyword and loops
  • The "if" keyword and conditionals
  • The "while" keyword and conditional loops

Listening & Learning

Karel is a good listener and can answer the following questions:

front_is_clear() front_is_blocked()
left_is_clear() left_is_blocked()
right_is_clear() right_is_blocked()
beepers_present() no_beepers_present()
facing_north() not_facing_north()
facing_south() not_facing_south()
facing_east() not_facing_east()
facing_west() not_facing_west()

This is listed in the Karel Supplement - you may want to bookmark it for now.

How to ask

Karel can answer, how can we ask?

  • Introducing the "if" statement.
  • We can ask Karel to check if, say: no_beepers_present()
  • Before we ask Karel to, say: put_beeper()
  • We imagine this prevents multiple pothole filling bots from doubling up on work.
  • We use the 'if' statement to do this. if beepers_present(): put_beeper()
  • As with "def" and "for", we have parens "()", colon ":", newline (enter) and indent (tab)

Potholes

  • We can imagine potholes as gaps in beeper coverage over the bottom/south of our grid.
  • We can write a "fill and move" function to place a beeper (tar) anywhere there isn't a beeper while moving across the bottom/south of the grid.

Potholes ⇒ Notholes <slaps knee>

def fill_and_move(): if beepers_present(): put_beeper() move() def main(): for k in range(4): fill_and_move()

How to ask

Karel can answer, how can we ask?

  • Introducing the "while" statement.
  • As in, "keep working on <something> while it isn't finished"
  • We can ask Karel to check if, say: front_is_clear()
  • Before we ask Karel to, say: fill_and_move()
  • This allows us to "pave" with beepers a road of any length!
  • We use the 'if' statement to do this. while front_is_clear(): fill_and_move()
  • As with "def", "for", and 'if' we have parens "()", colon ":", newline (enter) and indent (tab)

Potholes ⇒ Notholes <slaps knee>

def fill_and_move(): if beepers_present(): put_beeper() move() def main(): while front_is_clear(): fill_and_move()

Exercise

  • Do "Problem Set 1: Karel"
    • Use comments
    • The first problem "newspaper" benefits from functions, and perhaps for loops
      • It is possible, but annoying, to do without either
    • The second problem, "painting" benefits from 'while' loops (and could use 'if')
      • I used four (4) 'while' loops, and no 'if' statements or 'for' loops

Announcements

  • By FRIDAY 11:59 PM: Fill out this survey
    • There are weekly, 1 hour meetings with peer tutors on Wednesday or Thursday.
    • Attending these is 10% of your grade.
    • Read the instructions carefully and fill out as many times as you can!
  • By MONDAY 11:59 PM: "Introductions" assignment.
    • A minimal problem set to familiarize yourself with how to submit coding assignments.
    • Links will be posted on the course website and on the Discord.
      • They will not be posted on Canvas or via email to help encourage you to use the website and Discord
  • By NEXT MONDAY 11:59 PM: "Problem Set 1: Karel" assignment.
    • One problem (probably) using "def" and "for"
    • One problem using at least "while" and maybe the other three.