Lecture Materials


Class Announcements

  1. Assignment 1 will be released on September 16th.
  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: Is there a way to set a loaded world so that everytime you change your code and save, you don’t have to load the world again?

A1:  No sadly :(


Q: whats the advantage of separating ascend and descend into diff functions?

A1:  Style! And also you can reuse the function multiple times without rewriting any code. Saves you time which is precious.


Q: Couldn’t he have used if/else. like what if it was if blocked: move, else: turn_right??

A1:  That would only move Karel once. We want Karel to move to the top of the Steeple.


Q: when we make new commands like ascend hurdle, do we have to write explanations for it for humans to read? and where would we put that?

A1:  Yes! Please write explanations for humans to read! As Mehran just said, put them inside triple quotes at the top of the function.


Q: Is it recommended to write out all the necessary code first then find ways to group them and define new functions?

A1:  It is better to slow down and think about how to decompose before you start coding.


Q: Is there such a thing as too many functions?

A1:  Yes, there is such thing as over decomposition. But it is better to decompose as much as you can and your SL will tell you if it was too much.


Q: as much as I love the comments, is there a way to mute them? They're super distracting!!!

A1:  Not that I know of, but you can close the chat window.


Q: can Mehran make this window bigger for next time please?

A1:  Yes I will let him know.


Q: What exactly is "i" in that for loop?

A1:  It is a counter that starts at 0 and increases 3 times.


Q: Does it matter if the definition is before or after your main code?

A1:  No.


Q: how will we figure out in what order to put the functions?

A1:  Check out the style guide that we will post on the website!


Q: is it bad practice to define main() after all of its helper functions?

A1:  We usually like to have main at the top.


Q: Is it okay to do a # comment instead of a “”” when you’re defining a function?

A1:  It is more common to use “”” when defining a function because they will likely be multi line comments.


Q: Is it preferred for comments to be very descriptive or more concise?

A1:  More concise.


Q: Can you audit CS 100A?

A1:  Yes! Email Sonja to do so.


Q: So is the way we should approach assignment one to think through the concepts and decomposition first (what functions we will define) and then trying to write that code?

A1:  yes exactly !!


Q: should pre/post condition comments be written before every helper function?

A1:  Yes in Karel!


Q: Can we get Karel to pull off some dance moves later in the course?

A1:  You can! Think about entering the contest!


Q: Logistical question: Do we have access to the questions asked and answers given to each lecture during the zoom seminar after the seminar is closed?

A1:  Yes!! they will be in the same place as where we post the lecture slides!


Q: What does "i" in the for loop mean? Thank you!

A1:  It is a conuter to count how many times the loop is repeated! It start at 0.


Q: How do we know when to add colons

A1:  You use colons when defining functions, loops, and conditions.


Q: For comments for functions, should we identify pre-condition and post-condition like you did or can we just generally decribe what the function does?

A1:  Please define pre and post.


Q: For style, would stating the purpose of the function, the precondition, and the postcondition in a multi-line comment for every function (except for main)be sufficient ?

A1:  yes ! That is perfect.


Q: Is CS100A the same as ACE?

A1:  yes !


Q: When downloading PyCharm, it's giving me installation options:

A1:  Post to Ed!


Q: Karel turing left is a right for us, correct? The left & right trick me up

A1:  Karel turning left rotates karel 90 degrees counter clockwise.


Q: 64-bit launcer, add "open folder as project," .py and "add launchers dir to the PATH." do I not mark the checkboxes for these?

A1:  Post to Ed if this is not in the handout.


Q: how do we change the units form 3 to 5?

A1:  You have to drop and then re enroll for the correct number of units.


Q: I’m getting an error when trying to access Lecture 1 slides from the website.

A1:  Thanks for letting me know! We will get that fixed.


Q: if we're on a flex quarter, can we unofficially take cs100a?

A1:  You are welcome to audit CS106A. Send me an email if you want to do this. Auditors have access to the course videos. However, auditors cannot go to discussion sections, nor sumbit assignments, nor take either of the diagnostics.


Q: If you have another version of python installed, what do you do?

A1:  We highly recommend using python 3.8, which can be installed in parallel with other versions of Python.


Q: should we download it now?

A1:  Wait until after class! Lots of interesting things to learn in lecture today!


Q: How can we see the reading assignments for each lecture?

A1:  We usually mention in lecture!


Q: I took Code in Place in the spring, should I let the teaching team know since some of my assignments might be similar to what I turned in for that program?

A1:  Yes. Please let the teaching team know. Read the honor code closely and reach out if you have any questions on this.


Q: would we be allowed to use for loops, because they're after section 4 in the karel course reader?

A1:  You can use anything that is mentioned in the Karel Course Reader.


Q: Is there a calendar on the website to stay up to date on assignments?

A1:  Yes! Go to the Class Schedule page on the website. There is a link to it on the right sidebar of the website.


Q: Is there a worksheet for assignment 1? (what we should complete for each question).

A1:  You will write python code that instructs Karel to solve each problem. You will turn in the python files that contain this code.


Q: About how much storage should we have available for this class?

A1:  You dont need much! Almost everything beyond pycharm is a textfile / image.


Q: How is “”” different from a hashtag?

A1:  A hashtag comment only lasts one line. “”” allows you to have a multiple line comment.


Q: Does the function name “main” have any significance or could you have titled it anything?

A1:  It has significance! We will explain that more in depth later in the class.


Q: do we get these slides in canvas after class?

A1:  They are posted on cs106a.stanford.edu in the lectures tab.


Q: So we can’t refer to i inside the for loop?

A1:  Karel can’t, but next week we will start encorporating i


Q: what does the # note indenting mean?

A1:  That is just a comment to explain what is happening on that line of code.


Q: does i always start at 1?

A1:  i always starts at 0.


Q: Is there a space between () and : after every for loop/function definition line?

A1:  there is no space between () and :


Q: What’s the significance of the closed brackets after each function?

A1:  The parantheses after the name of a function tell python to actually execute that function.


Q: do you have to close loops, functions, etc in python?

A1:  nope!


Q: What’s the i for? for ‘i’ in range(4)

A1:  We will talk about this in depth later, but you can think of it as a counter — or you can ignore it for now and wait until next week. Just read that line as “repeat 4 times”


Q: i is the function we are referring to?

A1:  You can read this line as ‘repeat 4 times’. We will talk about i in depth next week. But for now you can think of it as a counter!


Q: what is the name of the resource prof sahami mentioned at the start for students who want additional help w computing/CS?

A1:  CS100A! There is an announcement about it on the website!


Q: If i starts at 0, wouldn’t you be repeating it technicallly 5 times (i = 0, i = 1, i = 2, i = 3, i = 4)?

A1:  it doesn’t let i = 4. Its like “while i is less than num”


Q: if you use the put_beeper function but Karel doesnt have a beeper will you get an error or does Karel ignore it?

A1:  Karel has infinite beepers. !


Q: How do you now if Karel is carrying a beeper?

A1:  Karel always has infinite beepers in its bag.


Q: Is the tab key used for indenting?

A1:  Yes!


Q: Is it necessary to include code for the end of the loop, like >>end in Matlab for loops?

A1:  Nope! The indentation shows the way :-)


Q: Is there a max number for the count that Karel can perform ?

A1:  if you use a for loop karel will execute a fixed number of repetitions! while has no max


Q: What does # note indenting represent?

A1:  That is a comment explaining that the line is indented.


Q: When would it be advantageous to use a while loop instead of a for loop?

A1:  We recommend using a for loop when you know exactly how many times something will happen. We recommend a while loop for when you do not know how many times something will happen, but know a condition that must be met for something to happen.


Q: is the # note intending a recommended line to add in our code?

A1:  No. That is just to point out to you in this lecture. You do not need to include that in your code.


Q: how do we indent one level?

A1:  You can just hit tab once to indent one level.


Q: Is move_to_wall() a function we create or is it already programmed in Karel?

A1:  A function that you create!


Q: Would we need to define front_is_clear first or does Karel know that?

A1:  Karel knows front_is_clear()


Q: is front_is_clear a condition we can check w/ karel or do we have to define a way to check that first?

A1:  front_is_clear is a condition you can check without defining it first!


Q: why di some statements end with : while other don’t

A1:  We put colons when definiting functions, conditions, and loops.


Q: what does it mean in the for loop that the postcondition of the loop needs to match the precondition?

A1:  We want Karel to be in the same place at the beginning and end of each loop, so that the process is repeated properly.


Q: I thought Karel has infinite beepers?

A1:  I was mistaken. Karel is not always carrying infinite beepers and you can use beepers_in_bag() to check if there are any beepers in Karel’s bag.


Q: is it best practice to always define a test and it’s opposite?

A1:  not always. Sometimes you just want a test (no opposite)


Q: if Karel has infinite beepers how could no_beepers_in_bag every be true

A1:  I was mistaken! There are times at which Karel may not have infinite beepers. Sorry for the confusion!!


Q: Thank you! Can we also create conditions that consist of more than one condition? For example, front is clear AND beepers in bag?

A1:  sure. just use “ and ” or “ or ” between the statements


Q: when we use the while loop, can we also use the opposite? For instance, “while front_is_blocked”?

A1:  yes!


Q: when do we need to put semicolons at the end? Is it just after defining functions or conditions?

A1:  Yes! You need colons when definition functions, conditions, and loops.


Q: I am using Python 3.8.5, is that fine?

A1:  Yes


Q: Why are we calling the function “main”?

A1:  It is a special name for the highest level function in your program. We will get into why it is called that later in the course.


Q: How are we meeting precondition on this while loop if we put a beeper which was not there in precondition?

A1:  The precondition is met at the *start* pre climbing a steeple. Then Karel starts to do her thing and go over the steeple


Q: When will Karel not have beepers in its bag?

A1:  Sometimes Karel has a limited number of beepers in its bag.


Q: So we’re supposed to read sections 1-4 of the karel reader for friday?

A1:  Try to finish the Karel reader by Friday!


Q: the # on the right are for our understanding in the lecture right? not somehting we need to write in the code

A1:  Exactly !


Q: can you define new tests as you might new functions?

A1:  Tests happen inside functions.


Q: Would placing a beeper with an empty bag cause a runtime error? Should we not check to make sure there are beepers in the bag first? Or is this not necessary for the purposes of this lesson and we assume the beeper bag is full for our purposes?

A1:  It would be good to check before!


Q: When do we have to have the whole Karel reader done by?

A1:  Try to have it done by Friday!


Q: would we have to to note an “OBOE” with a # if we had one?

A1:  You can! But it is not a requirement.


Q: is adding a line outside the loop the best way to fix an OBOE

A1:  This is one common way to do it!


Q: How many years of school is that 😳

A1:  A lot.


Q: was it annnounced in class that we were supposed to already have read 1-4 of karel or were we supposed to have seen it on the website or something?

A1:  Check the to do list on the CS106A website. It is on the top left of the home page.


Q: Where can we find the list of commands karel already knows?

A1:  On the reference page in the Karel reader


Q: can you have multiple conditions in an if statement?

A1:  yes! You can use “ and “ or “ or “ to combine several


Q: When would you want to use ‘while’ versus ‘if’ ?

A1:  ‘while’ would cause something to continue happening until the condition is false. ‘if’ would cause something to only happen once if the condition is true.


Q: Sometimes the def says main and sometimes it defines a new function, how we do know when we’re supposed to define a new function and just call it in the main or why can’t we define everything in the main?

A1:  It is good style to define helper functions outside of main and call them inside of main.


Q: do we have to write #note indenting when we code?

A1:  Nope!


Q: is something like safe_pick_up() descriptive enough or would we need to add a # to explain in english for style

A1:  If you define a function, it is always good style to include a comment in english.


Q: Why aren’t there parenthesis after the else and only colon?

A1:  We do not need to check any conditions in the else case so we do not need to call any functions. The parantheses are present when we call functions.


Q: Can you just use multiple “if” statements instead of using “if-else” statements?

A1:  Yes. But, it is usually better style to use if-else.


Q: Does python have else-if statements? Or just else & if? Are we allowed to use them if so?

A1:  Python has else-if statements. Please only use what is in the Karel Reader for the Karel assignment.


Q: how do you stop an if/else statement?

A1:  You can just move out a level of indentation to get out of the if/else statement.


Q: will we have to define out own conditions for the statemetns in the future?

A1:  there is a standard set. You can see them all in the reader. front_is_clear and beepers_present are the most common


Q: Can we use elif in our assignment 1

A1:  yes!


Q: Is there a while-else control flow?

A1:  no :( sadly


Q: what is elif?

A1:  its something we will cover next week in depth (you dont need it for assn1). But it stands for “else if”. elif should be followed by a condition


Q: can we use && / || in python?

A1:  && is “and” || is “or”. python is easier to read than C++ or Java :)


Q: is the right defined as a universal “east” or is it relative to karel?

A1:  Turning right rotates Karel 90 degrees clockwise.


Q: does while_right_is_blocked always refer to the east even when karel is facing a different direction?

A1:  No. It refers to right relative to Karel.


Q: When do you add colons and when do you leave them out?

A1:  colons start a code block (eg the body of an if statement, a while statement or a for loop).


Q: Would the orientation of "right" change as Karel turns left? Or Would "right" always be to the right?

A1:  ‘right’ changes as Karel rotates.


Q: Would this mean that Karel’s directions (right or left) change every time Karel turns? Would it matter which way Karel is facing when Karel checks for walls?

A1:  Yes! and yes!


Q: Are we going to have to define turn_right in our program?

A1:  Yes!


Q: what is the key difference when choosing between while loop and if/else statement?

A1:  While loops will repeat until the condition is false. An if/else will only execute once.


Q: If we didn't know the location of the first steeple would we need to start with a move_to_wall() function before the turn_left()?

A1:  Yes exactly!


Q: Sort of a silly question - may have been covered before. Is there an easy way to remember which way is Carol’s ‘head’ / heuristics for understanding which way Carol is facing?

A1:  Not silly! Suprisingly hard. Imagine Karel’s world is birds eye view and the front foot is pointing in the cardinal direction (NWSE)