Lecture Materials


Class Announcements

  1. Assignment 1 will be released today.
  2. Section signups: The section signup form will open tomorrow at 5pm. Sign up before Sunday at 5pm.

Questions & Answers


Q: When/how do we sign up for section?

A1:  There will be an announcement posted on the course website tomorrow at 5pm PT with a link to list your time preferences for section


Q: Quick question on the assignments. For the bit code, do we need to “submit” anything, or leave it complete once it runs correctly for each of the problems?

A1:  Nick will explain this at the end of lecture !!


Q: how is if different from while

A1:  Great question! bit will do something once if the condition is true. Bit will do something repeatedly while the condition is true.


Q: what is the functional difference between if and while?

A1:  If happens only once if the condition is true. While happens repeatedly while that condition is true.


Q: lol sorry i forgot, but are the lectures posted on canvas or ED?

A1:  Lectures are posted on canvas under the Panopto videos tab!


Q: can you have more than one while/if statment in a code?

A1:  Yes! You can have as many as you want in a function.


Q: what does “clear” mean in while bit.front_clear

A1:  bit.front_clear() checks if the square in front of bit is clear. This will be true as long bit is not next to a wall.


Q: What are the red lines on blue blocks mean?

A1:  That means that in the end state, those are supposed to look different than they do when it starts.


Q: is it possible to do everyone square not green

A1:  you can write a functiion to paint all squares that are not green.


Q: What is an expression?

A1:  An expression is a piece of code. So something like: if bit.get_color() != ‘red’ is an expression.


Q: what is the diagonal red line on the blue square again?

A1:  That means that in the end state, those are supposed to look different than they do when it starts.


Q: what is the difference in bit.paint and bit.get_color

A1:  bit.paint(‘blue’) paints the square that bit is on blue. bit.get_color() checks what the color is of the square that bit is on.


Q: none doesnt use apostrophe’s?

A1:  No you do not need those around None. It is a special keyword in python.


Q: can you have a double if-statement back to back?

A1:  Yes!


Q: how do I get the bit program? first day in class

A1:  On the course website (cs106a.stanford.edu) under Lectures->Lecture 2->Slides and then you can click the Fix Tree link to get to this example.


Q: why does bit start bottom left instead of top left

A1:  When you load bit (by doing bit = Bit(filename)) you decide where bit starts in the world. On your homework it will be clear where bit starts from the written description above it.


Q: where’s the bit guide with all the functions bit can do?

A1:  Under the “Resources” part on the right side of the course website home page (cs106a.stanford.edu)


Q: how can we access bit? should we be following along on our own right now

A1:  You can follow along by going to the lecture notes and clicking the link “Fix Tree”. Up to you if you want to follow along.


Q: Hi Juliette, where can we access those notes Nick is currently presenting?

A1:  Hi! On the course website (cs106a.stanford.edu) under Lectures->Lecture 2->Slides and then you can click the Fix Tree link to get to this example.


Q: where do we find the lecture notes?

A1:  On the course website (cs106a.stanford.edu) under Lectures->Lecture 2->Slides and then you can click the Fix Tree link to get to this example.


Q: Is homework on canvas?

A1:  No it is on the course website (cs106a.stanford.edu) under the Assignments tab


Q: Could we use a for loop instead of the while loop?

A1:  Great question! In this example, we know how many squares there are before the red square. But we want to write a function that will work generally for any number of squares before the red square. So a while loop is the type of loop we want here since we don’t know the exact number of times bit will move before the red square.


Q: How do you get the digonal red lines and is there a software/program that can predict what your code will do before you run it?

A1:  They should be on the picture. And there isn’t software that can predict before you run code. But maybe someday you could write that!


Q: Why would you want a while loop versus an if loop?

A1:  Great question! If doesn’t loop, it just happens once if the condition is true. While happens repeatedly while the condition is true.


Q: can we keep writing code on the same line after putting a colon?

A1:  You should move down to the next line after you put a colon.


Q: while get_color == none would work too right?

A1:  That will work in this case because the squares happen to be blank, but we want this program to work even if the squares were some other color.


Q: why does it start with def and (filename)?

A1:  def is how you start writing your own function! We will learn more about this soon. The filename is the world that bit is in.


Q: should we be following along w/ bit coding real time? or just watching lecture?

A1:  Up to you! Today is pretty fast paced. Maybe try following along on some and just watching for others.


Q: why can’t it be while front is clear?

A1:  Because we want bit to stop when it reaches a red square.


Q: Why can’t we use the “while bit.front_clear()” command we used before?

A1:  Because we want bit to stop at a red square rather than at a wall


Q: how we submit the homework?

A1:  We will explain more details about this later :)


Q: do spaces really matter when coding or not so imporant?

A1:  Spaces and indentation matter.


Q: would we need to specify direction in our code?

A1:  bit.right() and bit.left() rotate bit 90 degrees in the specified direction


Q: Is it also okay if we say while bit.get_color() == None: ?

A1:  That will work in this case because the squares happen to be blank, but we want this program to work even if the squares were some other color.


Q: Sorry I still don’t quite understand the diagonal red lines on the pic rn

A1:  They are there to show you which spots should be changed by the end of your program.


Q: When doing the problem on your own, how do you enable it to show the picture

A1:  If you are getting an error instead of the picture it is likely there is some problem with the syntax of your code.


Q: I have the exact same code as Nick but I’m getting an error that says “name ‘bit’ is not defined”

A1:  Where are you getting this error?


Q: is there a way to say front is blocked or is there only a front is clear function?

A1:  There is not a front_blocked() function.


Q: for some reason the bit tree program wont show the image of the tree/bit for me

A1:  There might be a syntax error in your code.


Q: Follow up to question about while front is clear: When it hits a red, why wouldn’t while front is clear code return false?

A1:  A red painted square is still clear for bit to move onto. Only a wall blocks bit.


Q: Is it okay to put bit.paint before bit.move?

A1:  It depends on your loop condition. Try it out


Q: on the right where the image of the tree and the world should be; it says Case: Compile Error and then Name Error: and then that

A1:  That usually means you have a syntax error in your code


Q: cant you just swap the last two lines instead of the if statement

A1:  Try it out!


Q: can you please explain the while bit.get_color () != green line?

A1:  That means the color of the square that bit is on is not equal to the color green.


Q: could you have just switched the order of the move and paint commands to achieve the desired result??

A1:  Try it out


Q: does the indentation happen automatically or do we have to pay attention to it manually?

A1:  Some editors indent manually, but you should pay attention to your indentation levels.


Q: What do the empty brackets represent?

A1:  the parantheses mean ‘call this function’


Q: why can’t you just say if None, then paint red? instead of if its not green … ?

A1:  Because we want this code to work even if the squares were blue instead of None.


Q: why does using bit.front_clear() paint the green squares too?

A1:  A colord square does not block Bit, only walls block bit.


Q: will this work too? so without the if while bit.get_color() != 'green': bit.paint('red') bit.move()

A1:  Try it out!


Q: So in this case, we could have also done while...color == none with bit.move() and bit.paint('red') under it without an if statement?

A1:  Try it out!


Q: why does the code need the second while loop?

A1:  To paint the top of the tree


Q: is this book good? Has anyone read it before?

A1:  I haven’t read it. If you read it, report back :)


Q: How do you know code needs a particular indent before getting an error message?

A1:  Inside an if statement, a for loop, or a while loop you need to indent your code


Q: It says i have an indentation error but my function is identical to the solution?

A1:  You may have extra spaces rather than tabs or vice versa


Q: why do we need to put bit inside () of go_west function?

A1:  the go_west function needs to know which bit to use in its function. You don’t need to worry too much about this now.


Q: when not in the big world would call by name be go_west()?

A1:  yes, go_west(bit) is call by name


Q: do you have to define a function to use its name?, i.e. use "def"?

A1:  Yes! You have to define functions somewhere before using them.


Q: Is there a way to instantly break a while loop without proceeding? ie if red then “end” or “break”?

A1:  yes there is a break statement in python. You can do: if bit.front_clear(): break


Q: so is bit.go_west() equivalent to go_west(bit)?

A1:  Those are not equivalent. It depends on how the funcitons are written. The inventors of bit wrote bit.left() which is why we use noun.verb. We wrote go_west which is why we call by name. Any functions that we write are call by name.


Q: Why does the definition of go_west have parantheses with bit: go_west(bit)

A1:  the go_west function needs to know which bit to use in its function. You don’t need to worry too much about this now.


Q: So could you write left(bit) and get the same result as bit.left?

A1:  Not quite. Passing in bit only works when you write the function yourself. If you define a function called left then you could do this.


Q: When do we need to put ‘bit’ in the pranthesis?

A1:  You don’t need to worry too much about this right now, but when writing funcitons in bit you probably always want to pass bit into the paranthesis.


Q: How do we know when to use ‘bit’ or ‘Bit’?

A1:  you should always use bit.


Q: Do you only need “def” at the start of your code when creating a new function?

A1:  yes


Q: Are these notes accessible to us?

A1:  Yes ! On the course website under Lectures -> Lecture 2 -> Slides


Q: why are there 2 ways of calling a function and not only 1?

A1:  It depends on how the function is written.


Q: Can you write bit.go_west() or does it have to be go_west(bit)? I.e. can you use the 2 ways to call functions interchangeably?

A1:  You cannot use them interchangably. It depends on how the functions are written.


Q: is there a limit to how many functions you can define? and do you always define functions at the beginning?

A1:  There is no limit. And in python it does not matter where you define the functions. It is good style to always define them in the same place (either the beginning or the end) depending on which you prefer.


Q: Are we supposed to be doing these programs as Nick does them or is it ok if we just watch?

A1:  Up to you! Maybe try following along on some and watching for others.


Q: when do we use bit.function() versus function(bit) ?

A1:  It depends on how the functions are written. We will talk about this more soon.


Q: How does bit.front_clear() work when the square is blue?

A1:  A colored square is clear. Only a wall blocks bit


Q: is there a name of bit.left()? Can we use a different way to call it?

A1:  no, bit.left() was written by the inventors of bit, so we use noun.verb. fill_row_blue was written by us, so we use call by name


Q: What is the line beginning with # doing?

A1:  That is a comment! You can use them to write notes to yourself and people reading your code. Any line that starts with a # is a comment and isn’t considered code when you run the code.


Q: How do you find the precondition of a function?

A1:  The precondition is where bit is in the world, and where it is facing, at the beginning of a loop. Usually you have to trace through the code to figure this out. Sometimes the problem will tell you.


Q: When do you use a colon :

A1:  When you use an if statement, a for loop, or a while loop. Also when you define a function.


Q: Why are we running bit = Bit(filename) in this function and not in the previous one?

A1:  We want to create the world for bit only once. So we only need to call it in the outermost function


Q: what happens if the pre-condition isn’t met? Like if bit is facing down? Will it colour the first column? I’ll give it a go after class :)

A1:  Then your loop probably won’t work as expected. You want to make sure that when you get back to the top of a loop you have the necessary pre condition


Q: why does bit have to rotate left, can bit just stay the same orientation the whole time

A1:  bit.move() moves bit forward one square in the direction it is facing


Q: wait how is he calling fill_row_blue. doesn't "def" in that second set of code define a new function instead of call the first one?

A1:  Yes! There are buttons at the top which choose which function to call when you hit the run button


Q: What is the purpose of bit = Bit(filename)

A1:  it creates the world that Bit will be in


Q: Is there anyway to get the code to snake through or does bit have to be in the same initial position?

A1:  You can write a function to snake through!


Q: What code allows bit to move between rows?

A1:  When bit moves right twice and moves


Q: why wouldn’t you just tell bit to move down after it turns left in the “fill_row” function instead of where it is now right before you call the “fill_row” function?

A1:  There are many ways to solve this problem! Try out your ideas


Q: Can you put fill_world_blue first instead of fill_row_blue?

A1:  Yes, the ordering of defining functions does not matter.


Q: How does the program know to start with fill_world_blue and not fill_row_blue?

A1:  There are buttons at the top of the problem where you can choose which function to call when you hit the run button


Q: beginning a coding line with ‘def’ doesn’t actually cause the bit to do anything, it just defines a function for later use right?

A1:  Exactly!


Q: When is assignment 1 due?

A1:  Next Wednesday 11:55pm


Q: why did the function fill_row_blue in the first example run when it was defined? Does a function always run when it is defined?

A1:  There are buttons at the top of the problem where you can choose which function to call when you hit the run button


Q: Which part of the code make it go to the next row? Does it do that automatically?

A1:  Turing right twice and moving


Q: Should you memorize functions?

A1:  No you do not need to memorize functions


Q: Is bit.clear affected by the color of the space or does it only check for if it is blocked by a "world barrier"?

A1:  Only a wall blocks bit. A colored square does not block bit


Q: When I try to work the examples, I cannot see what I am doing on the right, there is no visual of the case. How would I get that?

A1:  There may be an error in the syntax of your code. If you think this is not the problem, post on Ed after class and we can try to fix it.


Q: Why did the code start with def fill_world_blue(filename): instead of starting with def fill_row_blue(bit):?

A1:  Sometimes we want to take in the filename so that we can create the bit robot and the world (bit = Bit(filename) creates bit and its world). Othertimes bit and the world are already created so we just pass in bit itself.


Q: why is something happening with the code right now even though all commands begin with ‘def’?

A1:  When you hit run, it runs one of the functions.


Q: Why does it paint that last square green even when the right is clear?

A1:  it moves before checking if the right is clear again


Q: When the assignments say they're due Tuesday/Wednesday at 11:55 does that mean you get the 2% bonus for turning it in on Tuesday or Wednesday?

A1:  No that means that some weeks the 2% bonus will be for turning it in on or before Tuesday and some weeks it will be for turning it in on or before Wednesday. Sorry for the confusion!


Q: why cant you just add those arrangements directly in the helper?

A1:  It is good practice to have functions for each individual task in your code. This makes is easier to read and debug.


Q: Is there a dictionary of all the bit.functions (bit.move, bit.left, bit.paint) available to us?

A1:  Checkout the Bit Reference under resources on the right side of the home page of the course website (cs106a.stanford.edu)


Q: Is there a way to loop the parts of cover_square to make it more efficient?

A1:  Try it out! There are lots of ways to solve this challenge


Q: Could we write some kind of loop to accomplish cover_square ?

A1:  Try it out!


Q: Whe you def a function will it automatically run? Or do you have to call it outside of the def? i.e. wht is it not running on lines 1-5?

A1:  You have to call it. But when you hit run in these examples, it runs the function


Q: can we put bit.right in the function a to make more “clean”

A1:  Try it out! Sorry I forget the exact example you are referring to


Q: Why do we write cover_side(bit) instead of just cover_side() when defining the function?

A1:  The function needs to know which bit to use when running this function!


Q: could you use a for loop 4 times for the cover problem?

A1:  Try it out!


Q: I clicked the link to work on bit problems, but it does not show the box where bit is moving/any of the colors

A1:  Try running the code! There may be some syntax errors.


Q: how do we submit an assignment

A1:  We will explain this in more detail a litte later :)


Q: for the homework, is there a way to submit it? or are we just done after we do it on the site?

A1:  We will explain this in more detail later :)


Q: Is there any homework aside from assignment 0?

A1:  Yes! We just posted the first homework assignment on the website


Q: Why couldn’t you just do a loop for the second part of cover_square instead of repeating the code four times?

A1:  You can try a for loop! There are many ways to solve this problem


Q: the assignment says you will explain how to submit the working code, will that be in a different lecture?

A1:  Yes!


Q: Can you explain how to do the homework (access, turn in) ?

A1:  Homework can be accessed from the website. We will explain how to turn it in during class on Friday. Make sure to log into your account.


Q: where can we find the first part of the assignment?

A1:  On the course website under the Assignments tab


Q: how do we submit our code on 1-5 when it’s done again?

A1:  We will talk about this on Friday


Q: how do we send the homework?

A1:  We will talk about this on Friday


Q: Just wanted to say thank you!!!

A1:  Thanks for coming!


Q: How do we submit the homework?

A1:  We will talk about this on Friday


Q: Sorry how do we submit assigmnets again?

A1:  We will talk about this on Friday


Q: can you elaborate on "let the helper do the heavy lifting means" when calling the function?

A1:  You want to split out logic into different helper functions. I like to think of it as having one function for each task that I want to do. This makes your code easier to write, easier to read, and easier to debug.


Q: then how we submit the homework...

A1:  We will go over this on Friday


Q: I might have missed it. How do we submit our code? or is it automatically saved on the server with our log-in

A1:  We will talk about this on Friday


Q: is the general format here 1) define your helper functions 2) build the world and call your helper functions?

A1:  Yes! Exactly!


Q: Hi! I was asking about this earlier but I seriously don’t know how to fix this: I keep getting a Compile Error that says Name Error: Name “bit” is not defined. The code I have in is literally the code that is automatically there when I click on the link from the lecture notes.

A1:  Post on Ed about this please.


Q: how do we save/submit the code we write for the hw?

A1:  We will go over this on Friday


Q: Sorry might have missed it, but did we go over how to submit code for the pset

A1:  It will be explained in Friday’s lecture


Q: The website itself doesnt show the picture of the tree when i first click the link. This is before I type any code

A1:  Okay post on Ed about this and we will try to fix it.


Q: where can we run bit ourselves?

A1:  You can try it out in your first homework!


Q: if you include a defined function in its own definition, does that act as a ‘while’ loop?

A1:  If you define a function on your own and then call it, it will only run once. You could put that function inside a loop if you wanted / if that helped solve the challenge


Q: In the HW problem, I cannot see the world. Is that normal?

A1:  There may be a syntax error in your code. If you don’t think that is the problem, post on Ed!


Q: i did this on my own and differently, why do the steps go back to the beginning over and over again once it gets to the end, acting like a loop? def fill_row_blue(bit): bit.left() bit.paint('blue') while bit.front_clear(): bit.move() bit.paint('blue') bit.right() bit.right() while bit.front_clear(): bit.move() bit.left() bit.move() fill_row_blue(bit)

A1:  Hi! Can you post things like this on Ed? It is tough for me to try to read code and debug in the zoom chat.