Lecture Materials


Class Announcements

  1. Assignment 1 is out!
  2. Section signups: The section signup form will open tomorrow at 5pm. Sign up before Sunday at 5pm.

Learning Goals

By the end of class you should know how to program and trace core programming control flow: for loops, while loops, and if statements. Tracing a program is when you can think through what it does, line by line.


Questions & Answers


Q: if I use two computers, can I use pycharm on both of those of should I download it on my primary computer?

A1:  Up to you! If you’ll be programming on both of them, install on both of them :)


Q: Quick question- do I need install some package to use Karel on PyCharm?

A1:  And to plus that answer — no, we will give you an assignment 1 project with karel already pre-packaged

A2:  We’ll post a handout after class today explaining how all of that works :)


Q: Can I audit this class?

A1:  Please email me


Q: when should we have karel read by?

A1:  Its very useful for the first assignment! You can chose the timing that works for you — but we recommend reading it as soon as you get a chance so you can be ahead of the class


Q: Do we get access to the Q&A after the live lecture?

A1:  Thats a good question — I dont believe so but will look into it. Q&A in Ed doesn’t go away


Q: which lectures are needed for the HW, or is it just the whole karel reader?

A1:  The first 3 lectures, which cover the same material as the reader! You’ll be able to start and make your way through most of the homework after today, and after Friday, you’ll be able to do it with better style


Q: Do we always need to use “i” in a for loop?

A1:  For now, yes - we’ll talk more about how we can be flexible with this later!


Q: when using the for loop, do we have to define i at all?

A1:  Just saying ‘for i in range(4)’ is sufficient

A2:  yes! And later we will talk about what i means, and how you can work with it. For now just read that line as “repeat a few number of times”


Q: why is there no need for underscores in the “for i in range” phrase?

A1:  the underscores don’t replace spaces in python in general. Function names can’t have spaces, so we use underscores there


Q: Do we have to call it “main”?

A1:  Good question! For now, yes - we’ll talk more about how we can be flexible with this later


Q: what does the i mean? do we always have to use the letter i

A1:  For now, it’s just a piece of syntax we need to be familiar with - we’ll talk more about how we can be flexible with this later!


Q: Is the indenting necessary for the program to work ? Or is it for style ?

A1:  NECESSARY!


Q: This might be an odd question, but doesn’t Karel have to pick a beeper to put a beeper? Or are those mutually exclusive?

A1:  Karel can sometimes start with beepers already in her bag :)


Q: so the for loop always starts with (i = 1) and ends at (count)?

A1:  It actually goes from 0 to count - 1 but you don’t need to worry about that in Karel!


Q: can you put multiple beepers on one square?

A1:  oh yea!


Q: is range() a function that works outside of for loops, or is it just part of the syntax of the for loop?

A1:  well talk about it in a few lectures so you can understand it fully — it is a funciton, but you should also think of the for loop as being programming for “repeat x times”


Q: If you wanted to put 100 beepers would you put range 99 since i starts at zero?

A1:  You’d do 100 - because you’d count from 0 to 99, which is 100 numbers!


Q: do you need to define conditions the same way you define functions? or is front_is_clear built in?

A1:  live answered


Q: how does karal know front_is_clear. It is not one of the four commands she know?

A1:  live answered


Q: do we have to define front_is_clear?

A1:  live answered


Q: how does it know what front is clear means?

A1:  live answered


Q: can you define "syntax" and "condition" for note taking purposes?

A1:  Syntax: the rules that constitute ‘valid’ Python. It’s kind of like Python’s grammar.

Condition: CS nomenclature for a yes or no question (yes is called True and no is called False)


Q: If the statement in the while loop becomes false (say a wall is reached), and the program moves on, does the loop reactivate if the following code makes the condition true again?

A1:  Great question! Once the loop has ended, it will never start again. Python follows code from top to bottom within a function and doesn’t go back up again unless you’re in a loop.

A2:  good question. quick answer no


Q: can you put multiple conditions in a while? like while front is clear AND right is clear AND beepers presnset?

A1:  yes! Thats a little advanced… and we will cover it next week… but for now.. just write lowercase and between them :)


Q: An earlier question stated: kaleda denton 01:48 PM If you wanted to put 100 beepers would you put range 99 since i starts at zero? Answer: You’d do 100 - because you’d count from 0 to 99, which is 100 numbers! My question: Does this mean that “for i in range (x)” works for integers 0~<x?

A1:  We’ll explore this in a lot of detail in the coming weeks but the short answer is you can get a for loop to start at any number and go until any other number :) For now, just think of ‘for i in range(n)’ as doing something n times


Q: How do you indent your code?

A1:  PyCharm will do it automatically for you but you need to press tab if you want to do it manually


Q: can karel count beepers on a spot?

A1:  Not easily :) It can do math in a really cool way, stop by office hours if you’re interested in finding out how!


Q: Could you have used a “for loop” for the most recent example? Since you know how many spaces Karel has to go?

A1:  live answered

A2:  You typically want Karel programs to work for all sizes of worlds!


Q: does if_beepers_present only checkfor beepers when karel is at a corner?

A1:  Karel is always at a corner!

A2:  only check on karels current corner!


Q: if there are multiple beepers present, would it pick up all of them?

A1:  no just one!


Q: Do we then need to repeat the safe_pick_up command each time Karel moves to a tile?

A1:  Depends on what you’re trying to do in the problem :)


Q: So for an if statement, will it always only check the condition once?

A1:  Yep! You’d use a loop for repitition


Q: Can we use the if statement with the condition front_is_clear for the previous example?

A1:  Which previous example?


Q: could we check multiple conditions with “else if”?

A1:  We’ll get there soon!


Q: Are the lecture slides posted on the website?

A1:  Yep! Check the schedule or the lectures dropdown menu


Q: The example where Karel was moving in a line and putting beepers at every intersection^^

A1:  Could you post how you can imagine doing that? ‘If’ won’t repeat the code


Q: How do we know when to use a while loop rather than an if/else statement?

A1:  while -> repeats, if -> check once


Q: could also write def main(): put_beeper() while front_is_clear() move() ?

A1:  Karel would only put a beeper on the first square here!


Q: do we have to actually write “# note indenting” in our code? or is that just prof mehram letting us know that’s what we’re supposed to do?

A1:  Just a reminder :)


Q: Does anyone know how to turn off notifications from the chat on zoom?

A1:  There should be a way to do it in your zoom settings


Q: Will the beepers inversion function fail if a corner has more than one beeper?

A1:  yes!


Q: what happens if Karel does not have a beeper, and executes put beeper? does the code error out?

A1:  yes. karel can start with beepers in her bag


Q: Hi all, this has already been answered but wondering how we mute chat notifications?

A1:  There should be an option in your Zoom app’s settings to do this


Q: is checking the right always to the right instead of in reference to the direction karel is pointing?

A1:  It’s relative to Karel


Q: why are you assuming Karel has to jump immediately?

A1:  yes!


Q: Couldn’t you also use if and else?

A1:  good question. it would only move once (it doesnt repeat)


Q: Should't it be 'while the bottom is blocked' since it rotated left once?

A1:  ‘right’ is relative to Karel. There isn’t a bottom_is_blocked function, but Karel’s right kind of is the bottom of the Karel graphic!

A2:  I would say: recall that karel is really birds eye view (its just drawn sideways). The right is actually checking whats right from birds eye view (aka West)


Q: what if the first steeple isn’t right infront of Karel? Why would Karel move left first if no steepe?

A1:  Try tracking through what the code will do without a steeple!


Q: but then the for loop could also be used, right?

A1:  only if you know how big the steeple will be when coding (here just one steeple of fixed size that works)

A2:  We don’t know how tall the steeples are


Q: Does Karel’s “right” mean the east-most part of Karel regardless of which way Karel is facing?

A1:  Nope, it’s relative to Karel. If Karel is facing south, its right is west


Q: is "right_is_blocked" always the east direction regardless of Karel's orientation?

A1:  Nope, it’s relative to Karel. If Karel is facing south, its right is west


Q: is there a way for karel to check what avenue its on? for spaces where there's gaps (clears front clear) how does karel know the difference between move foward since there's another steeple coming up or stop bc its on avenue 9?

A1:  Karel can’t check what avenue it’s on :) try tracking through what would happen in the code if there’s no steeple


Q: is it better to define commands like turn right and move to wall at the beginning or when they are used?

A1:  you have to define methods one after another — so it can go above or bellow main but not in it


Q: If we had not defined move_to_wall before, would we define it within the code block shown or should we type it at the very bottom (outside of main)?

A1:  It should be outside of any other functions


Q: where do we write the def of move to wall does it have to be at the bottom like that or should it just go under turn_right()

A1:  It should be outside of any other functions


Q: Since ascending and descending the hurdle should be the same # of moves, could we just keep count of how many times we moved while ascending the hurdle and then use a for loop to descend the backside of the hurdle? Is this less efficient/stylistically inferior to always checking the front, as I assume Karel does in move_to_wall?

A1:  Karel doesn’t know how to count :)

A2:  but programmers can count :-). Mehran is just about to blow our minds by showing this working on different sized hurdles


Q: Can we use karel’s position as a condition for a while loop ? (as while karel is not yet at the 9th avenue…)

A1:  Nope, Karel doesn’t know where it is in the world


Q: can karel tell the difference between a hurdle and a wall? if I say move to wall, does he know to move and stop at the hurdle, or will he reach the hurdle and crash?

A1:  front_is_blocked works! a hurdle truly is a ‘wall’

A2:  Hurdles are made out of walls!


Q: Can you expain what move_to_wall() means

A1:  Move until you’re blocked by a wall

A2:  its a function he wrote earlier! def move_to_wall(): while(front_is_clear()): move()


Q: does the order in which you list functions after the main one matter? like what if we put the descend function before ascend

A1:  We’ll talk more about this later, but the short answer is no, as long as you’re consistent. It’s a question of style and not functionality.


Q: Is there a way to turn off chat notifications on my end for just this zoom webinar and not other zoom meetings? (to avoid distractions during lecture)

A1:  Not that I know of :/


Q: Is this function built into Karel?

A1:  move_to_wall is something you have to write


Q: i thought we were not allowed to create new variables? or is it ok to define new variables if they use karel’s built in variables?

A1:  Which variable are you referring to?


Q: I can't seem to shut the notifications off, although I've clicked off notifications?

A1:  i opened my chat and then the notifications stopped. I then put the chat window somewhere it wouldn’t bother me


Q: If one user defined function calls another, does that function need to be after the one it calls?

A1:  great question! no. any order is ok


Q: why not make one big function that’s called jump_hurdle or something

A1:  We did!


Q: as in defining ascend_hurdle(): as the turn_left, while right_is+blocked():, etc section of code

A1:  Those aren’t variables, those are functions!


Q: Why do you have to indicate what the pre-condition and post-conditions are?

A1:  no, but its good style


Q: sorry i mean—why bother defining ascend and descend if they’re just intermediaries to jumping the hurdle?

A1:  Oh, I see - it just makes the code a little easier for a human to read :)


Q: Got it, just kind of confusing because karel's "right" looks like its front foot

A1:  agreed!


Q: Do we have to write/explain the pre condition and post condition for every def we write?

A1:  There will be some functions that don’t have them, but if they do, you should clarify what they are.

A2:  its good style. I would recommend it when you are first getting started


Q: was move_to_wall defined?

A1:  Yep - the code should be in the slides


Q: jump_hurdle calls ascend_hurdle and descend_hurdle. Does it matter the order in which these three functions are defined in the code?

A1:  Nope


Q: do we have to write the pre and post conditions in comments for every addtl function we write

A1:  I would recommend it! Good style


Q: How does Karel know to stop at the ninth avenue and not go on to the tenth

A1:  The for loop is only calling jump_hurdle 8 times!


Q: why did we say (8) in the i in range function again?

A1:  We want to jump 8 hurdles!


Q: Do functions need to be defined before the main() body? ( I assume not, just checking)

A1:  Nope, their order doesn’t matter

A2:  no :)


Q: why is it for I in range (8) instead of 9?

A1:  We want to jump 8 hurdles


Q: Where can we access ed?

A1:  There’s a link on the course page!


Q: why is the for loop in main have a range 8 and not 9?

A1:  it wouldnt work if there was a 9th steeple.. your right

A2:  You start on location (1,1) so you need to move ahead 8 times to get to location (9,1) which is where the steeple chase ends.


Q: So we have to determine the number of hurdles for each unique world, it can't be general in that way?

A1:  live answered


Q: How can we correlate this exercise with what we program in Pycharm?

A1:  Check out the handout we’re just about to post!


Q: when we write the program, do we write it in that main space or in the terminal?

A1:  in the “text editor” area


Q: can you review when preconditions and postconditions are necessary? Like when to define them and when they have to match?

A1:  live answered


Q: And then similarly for if-else statements, it only checks the condition once?

A1:  your statement is correct!


Q: if a steeplechase wasn’t defined to be 9 avenues long, would we have to use a modified code with a while loop while checking to see if the front is clear?

A1:  Yep!


Q: so I was thinking about counting beepers on a spot, is it possible to have a counter within a for loop counting as many times karel did it, or is that a variable

A1:  live answered


Q: Does the function definitions have to come in a certain order?

A1:  no!


Q: Will the program go back if a condition becomes true even if we had already passed that loop?

A1:  Nope! once a loop is over, it’s over for good. Python goes from top to bottom inside a function unless there’s a loop/


Q: is the indentation equivalent to a "tab" when writing the code in the text editor?

A1:  Yep!


Q: Do we want to always write code with the intention of it being applicable in any world?

A1:  Unless we specify otherwise :)


Q: Although you start by writing File: xxxx.py, it is in quotation marks, so where do you actually name the file?

A1:  good question — the name is the name of an actual file on your computer. you can see and change the name in the panel on the far left


Q: I've tried turning off chat notifications in settings but it still notifies me of an incoming chat every time. Could we potentially disable the chat during lecture? It's really distracting

A1:  live answered


Q: I am kind of confused as to what a condition is, is it just the orientation of Karel?

A1:  It’s just a yes or no question that you can ask whilst programming - check out the section in the course reader for more details!


Q: you said there are some commands known by Karel, the same will happen in Pycharm, right? How do we knoe them?

A1:  We’ll get there very soon :)


Q: can we access the PPT of the class?

A1:  The slides are posted on the course page


Q: Great thank you, and in some of Professor Sahami’s Karel code, the pre and postconditions didn’t match. Why is that?

A1:  live answered

A2:  The pre/post conditions should match if we are looping through something. They pre/post conditions for each function don't need to match.


Q: One thing you can do is pop the chat out and move it to the edge of the screen where you can’t see it.

A1:  true


Q: Brahm - I believe you can change that chat notification settings - however, if you apply the new settings to the chat, it will not apply until the next time you open zoom.

A1:  Great insight, thank you!


Q: Is there a way to keep track of the Q&A and listen to the lecture at the same time? Sometimes it is helpful to know what other students are asking, but also hard to listen and read Q&A at the same time. =/

A1:  agreed! We will try and export it some how


Q: How can you determine if you have a precondition or not?

A1:  Anything that needs to be true for your code to work!


Q: Why did we not start the jump_hurdle() process with Karel checking her front?

A1:  i suppose we made an assumption that the front was clear — good catch


Q: just to double check, when we write the pre/post conditions, we're just describing how Karel is situated right?

A1:  Anything that is crucially true for a piece of code to be true!


Q: Would there be pre-conditions for if and if/else statements?

A1:  live answered


Q: Are the pre and post conditions just the direction Karel is facing?

A1:  live answered


Q: So, just out of curiosity, what would be a way to get a ++ on this assignment?

A1:  A really really cool extension!


Q: I’m not sure if this was already answered above but can you define function before writing the loop? In Mehren’s example he had all the definiton below the main if-else function?

A1:  The order of functions doesn’t matter


Q: Thx brahm and Chris! I understand that when you have a function that calls another funtion I defined, it jumps out of the big function to the inner function (or at least thats what I saw in the karel reader). What happens if like that gets stuck

A1:  live answered


Q: sorry for the last minute question my interntet is bad, don’t worry about it if you got to go

A1:  live answered


Q: What were people saying earlier about realizing the difference between a wall and hurdle at the very end? I didn’t catch it (If that is relevant/ something to be aware of?)

A1:  live answered