Lecture Materials

Class Announcements

  1. Section sign ups: Sections start this week. Section assignments will be emailed out to you by Tuesday. If you didn't sign up for a section, or need to change your assignment, a late signup form will open on Tuesday evening.
  2. Assignment 1 Due Wednesday: We have covered everything you need for assignment #1. It is due on Wednesday. Submission instructions will be available soon.
  3. LaIR is open: See the Zoom info link on the main page for hours.

Questions & Answers

Q: When will assignment submission open?

A1:  Once you get assigned a section leader which will be early this week.

Q: How much does it actually matter that our functions have the same pre and post condition? I have a function that mismatches the two bc it works in my code and while loops. If I wanted to mismatch them I’d have to add a function after every time I call this function

A1:  There are nod rules. If its a beautiful way of breaking down the problem, its good times :)

Q: What can I do if I’m very lost with downloading programs and navigating the first Karel assignment?

A1:  Get to the LAIR http://web.stanford.edu/class/cs106a/

Q: Stylistically, would it be better to write turn_left() three times, or write for i in range(3)?

A1:  I like the for loop option!

Q: Is looking up syntax for a python feauture, not code for a specific program, considered “looking up code” which is against the honor code

A1:  You can look up python syntax. But you can also find any syntax you will need on the course slides!

Q: Will pycharm automatically save your code after logging out?

A1:  You should hit file -> save all or cmd+s before closing pycharm.

Q: Where do we find out what sections we are in (i.e. what time and the Zoom links?)

A1:  Tuesday around 5pm PT.

Q: To clarify, it’s OK if pre and post conditions don’t match?

A1:  its ok

Q: If there are only two times during the sections that work for me, what will happen if I don't get into either of the two sections that I signed up for?

A1:  If you can’t make it to your assigned section for a given week, you can attend a different section!

A2:  We often are able to get everyone in a time that works

Q: I need the help from LAIR but it says no queues are open; what should I do?

A1:  Its not open at the moment. It starts in the evening

A2:  You can start signing up for LaIR starting at 4:30pm PT Sundays-Thursdays.

Q: Will not completing the optional MidpointKarel project impact our grade at all for the overall assignment? Just asking in case I do not have time to complete that specific project.

A1:  No! Its optional :)

Q: Should we put the comment inside the function or above

A1:  Often inside, but just having the comment is what matters :)

Q: In a previous example, there was a general description AND pre and post conditions before every function. Should we just pick one style or do both these comments?

A1:  Both is always the safest. But they are two (good) ways to describe the functions

Q: Should my newly defined functions be in any particular order?

A1:  No hard rule. I like to go from higher level (like main, jump_hurdle) and get more low level (like turn right) as you go down

Q: Should the comments be written before the def function() line or is it ok for it to be inside of the function?

A1:  Inside the function is preferred, but just having the comments is what matters.

Q: If we create a function turn_right() for example, do we need to make a comment to explain it or is it unnecessary? If so, should we put pre-and post- conditions in the comment?

A1:  At the start, comment more than you think is necessary. In the future you can pair back when you have more experience

Q: Is it ok to have while loops or if/else statements in the main function?

A1:  Yes! Try to decompose but if it makes sense to have these in main you can do that.

Q: Do the pre and post conditions of loops need to align as well as the pre and post conditions of functions?

A1:  Pre and post conditions for loops should align. Pre and post condiitons of functions often do not align.

Q: So the comment for your main function, all it has to do is describe and say what your code does right? It doesn't need a pre or post condition necessarily correct

A1:  correct

Q: Is it preferred that we comment our pre and post condition in our code?

A1:  yes!

Q: can we begin our program with a control flow? for example a while loop would follow def_main():

A1:  allowed!

Q: should we define our functions before or after our main function? is there a stylistic preference for one or the other?

A1:  I like to put main first and all of my function defintions below it. You. can also put all of your helper functions above main. I just recommend that you are consistent with how you choose to order your functions.

Q: Is there a reason why we cannot submit our assignments yet for Karel?

A1:  Yes! Once you have been assigned a section leader you will be able to submit.

Q: Is it possible to have too many lines on the first assignment? (should we decompose as far as possible or no?)

A1:  There is no line limit. Try to decompose as much as you can!

Q: What is a corner vs an avenue?

A1:  A corner is where a given street and given avenue intersect.

Q: What is the maximum length you suggest a comment for a function should be?

A1:  Several lines. Comments should be descriptive but concise.

Q: In the Karel assignment, can we change the name “main”

A1:  You should not change the name ‘main’

Q: what does it mean "print the "hello world" " to the screen?

A1:  In python, you can use print(“print this message”) to allow your program to output text.

Q: What’s the point of main? I haven’t used it before… I’d just place the functions and code and let it run.

A1:  When you run your program, main is the function that gets called.

Q: Why are there underscores before and after “name” and main” at then end?

A1:  We will discuss that later this quarter! Don’t worry about it for now.

Q: Is there any specific reason why Mehran runs his program through the comman line rather than clicking “Run” or “Run without debugging”?

A1:  You can do either!

Q: If I added a descriptive comment, should I still add a description of pre and post conditions?

A1:  Yes please.

Q: do we need a comment for every new function? for example when we define turn_right as turn_left turn_left turn_left, do we need to comment something like “karel needs to turn right” ?

A1:  Yes! Each function you define should have a comment.

Q: Do you not need eval(input(“”)) for integers?

A1:  You can just use input()

Q: what if you put in a non-number? How would it be converted?

A1:  When the program runs you will get an error at the int() line that says “invalid literal for int()…”

Q: for which functions do we comment the pre and post conditions?

A1:  All functions in Karel!

Q: can you breakdown which parts of num1 = input does what? For example, which part makes the text print, which part makes the cursor apear, etc.

A1:  Input(“Question: “) prints “Question: ” and then waits for a user to type input. Once the user types in their input, that value is sotred in num1

Q: do user inputs always initally go in as strings?

A1:  Yes

Q: How does python know how to add?

A1:  The inventors of python kindly wrote this feature for us to use. Come to office hours if you want to talk about how this happens on a lower level.

Q: So what you print cannot be in a form besides text (like an integer)?

A1:  You can print an integer!

Q: Do we need comments that explain what a function does or can we just have pre/post conditions?

A1:  Both would be best.

Q: Can't we make the function only accept integers for its input?

A1:  By the end of the q you will know how to do this!

Q: is there a space after the colon?

A1:  Yes! That way there will be a space before the user types their input.

Q: Do we always need text inside input()? Or can it be left blank

A1:  It can be left blank but that could be confusing for users because they would not know what to input.

Q: how does the program run the input command if it is changed to a number in the next line. num = input(x) then reassigned to num = 1, wouldn’t this just remeber the 1.

A1:  In this case, whatever the user typed in would be lost and num would be 1.

Q: Is the midpoint problem extra credit or just optional?

A1:  Extra credit!

Q: Are we allowed to use variables if it's applicable to the p-set for friday, or should we limit it to what we learned the first week?

A1:  You cannot use variables in Karel.

Q: In this class, are there partner assignnments or is it all individual?

A1:  All individual.

Q: are there shortcuts like ++ or -- in Python?

A1:  '++ and — do not work in python. You can use += 1 or -= 1.

Q: Can you create variables that are accessable to an entire program/ multiple functions?

A1:  Yes this is possible in python but you should not do this in 106A.

Q: What if a function is called in another function—are the variables moved as well?

A1:  Great question! They are not moved. Later in the course you will learn how to move these variables to other functions.

Q: So Python doesn’t require us to declare variables (string, int, double) before we intialize them?

A1:  Correct! How awesome is that?

Q: If you use the same character for different variables doesn't that make it semantically challenging for other people to read the code?

A1:  There are stylistic reasons to do use the same character for variables in different functions but for the most part you are correct!

Q: Can variables in python be used for multiple types of values, for instance x=int then later x= “string” ?

A1:  Yes!

Q: Do you have to assign variable types in Python?

A1:  Nope!

Q: Would you have to use the int() function to convert the num_in_class = 500 and num_students from an integer to a string before performing operations (i.e. subtraction)?

A1:  Yes!

Q: Could you store something like a fraction in a variable but not as a strong as numbers? Like something with an operator?

A1:  there isn’t a “native” variable type for this — but i have seen many programmers make their own. At the end of cs106a you will learn how to make your own variable types!

Q: are strings variables or objects? or values?

A1:  Great question! Strings are objects. A variable can store a string. A string can be the value assigned to a variable.

Q: can we put y = “10” with the double quotes

A1:  Yes. That would assign y to be the string “10”

Q: What is the number type called Double?

A1:  in other languages they distinguish between “floats” and “floats that get more memory” aka doubles… python gives all floats a lot of memory

Q: Why did the total(26) have to be converted to a string value in the last print statement in the program earlier?

A1:  you aren’t allowed to add a string and a number! so first convert the number to a string…

Q: if you divide two int’s and the quotient is not an int does python convert it to a float? ex. 7 / 2 == 3.5 or 4 ?

A1:  converts to a float!

Q: Would the pi (3.14…) be considered to be a float?

A1:  Yes!

Q: Do you need to use a separate line to define num1 as an int or can you just type int before num1 when you’re equating it to the input function?

A1:  You can do it in one step like: num1 = int(input(“Enter first number: “))

Q: Could we also print: print('The total is', total, ''.") or is there a specific reason he uses str()?

A1:  You can print that way as well. Mehran is creating one string rather than printing 3 separate things.

Q: what would happen if the input was text, and i tried to convert it into an integer

A1:  You would get an error when you try to convert it to an int that the input cannot be converted.

Q: could you type num1 = int(input(“Enter number here: “)) ? One line?

A1:  yes!

Q: so if you do num1 = float(num 1), will it be a real number then>?

A1:  Yes

Q: what is the difference between a number in a string (“9”) and an integer number (9)? in what situation would you use one over the other

A1:  If you have a number as a string, you cannot do any arithmetic operations.

Q: If you do print(“The total is “, total, “ .”), that would give you the same result without converting the int to a string right?

A1:  Correct.

Q: If the input number was a float would this program round it to the nearest integer?

A1:  Yes. The int line would do that.

Q: so what happens to the old “string” luggage? does it disappear

A1:  Great question! Come to office hours to learn more about this. For now you can think of it as disappearing.

Q: will assignments be given when the previous assignment is due or will we get them before?

A1:  The next assignment will come out the day the current assignment is due.

Q: so would a float be float(num2) ?

A1:  Yes.

Q: What would happen if you added 9 and 17 but they were strings and not integers

A1:  you would get “917”

Q: why is it string instead of int or float for the sum?

A1:  We convert total to be a string so that we can print it out as one string.

Q: Why did 26 have to be a string? can it not be typed as an integer in the terminal?

A1:  When you type into the terminal, it is stored as text.

Q: what if we continue with int version of total

A1:  If you do not covert total to a string, you will get an error telling you that you cannot concatenate a string and an int.

Q: If you left total as an integer value in the final print statement, would it result in an error?

A1:  Yes! It would tell you that you cannot concatenate a string and an int.

Q: What if you did int(4.5)? How does the floart get converted?

A1:  It removes the decimal. So this would covert that to 4.

A2:  it becomes 4

Q: Total’s variable type is unchanged because it is converted to a string within the print function?

A1:  Yes!

Q: can we use f-strings instead of concatenation?

A1:  Try to use concatenation in 106A.

Q: Do we always have to convert a number from integer to string in order for print to work?

A1:  no. only if you want to “concatenate” strings and ints

Q: is it impossible to print an integer directly?

A1:  yes. lets say x is an int… you can type print(x)

Q: Can you not print the total as an int within the printed string?

A1:  No because you cannot concatenate ints and strings. So this would result in an error.

Q: if you want to print just a variable, without adding it to any text, do you still have to convert it to a string? or can you just type the variable in the parantheses without the quotes?

A1:  no!

Q: If you then change str1 str2 or str3 will str4 update or does it have to be defined again as being str1 + str2 + str3

A1:  if you update str1, str2, or str3, after you declared str4, str4 will not update to have these new values.

Q: does the size of floats ever factor in to using integers over them if you have thousands of float variables, or is the difference always negligible?

A1:  sometimes it matters. Never in cs106a. But more often in applications like “ray tracing” or “deep learning” where you have trillions of variables

Q: How do I get to Office Hours? When I go through the website it is saying I don’t have the proper authentication

A1:  You have to log in with your sunet

Q: Why was the last one “x” + str() and this one just “x=“, x? Do we not always need the plus sign?

A1:  You can do either print(“x” + str()) which will concatenate “x” and str() into one string. You can also do print(“x=“, x) which will first print “x=“ then will print x.

Q: Do the commas automatically convert the following variable into a string?

A1:  it does!

Q: is it better style to use commas or + in print statements?

A1:  I like commas because it puts whitespace automatically

Q: why did we make total a string in the previous problem? Couldn't we could leave total as an int?

A1:  crashes! You can’t use the “+” operator with a string and an integer

Q: so the only difference between using + vs a comma in a print function is that there’s a space added if you use a comma?

A1:  yes

Q: does it matter if its 1 concatenated string vs 3 different print ?

A1:  three different print statements will have “new lines” in between them

Q: in the print part, when putting the total, why does it have to be str(total)? What happens if just “total” is entered?

A1:  You will get an error saying that you cannot concatenate ints and strings.

Q: Can you add floats and integers together?

A1:  yes!

Q: What is meant by base 10? Is that in contrast to binary?

A1:  Yes!

Q: Are we going to be assigned another assignment before Friday or is Assignment 2 not going to be assigned until next week

A1:  Assignment 2 will be released on Friday.

Q: should I email the cs190 or wait till tuesday to sign up ?

A1:  Wait until Tuesday please.

Q: Was Karel the only textbook for the class?

A1:  no, we have a new textbook, written by nick parlante. Just added to the website

Q: Can we use or in Karel

A1:  Yes!

Q: chris do you have office hours right now?

A1:  live answered

Q: Are we required to read/buy the textbook?

A1:  To read yes! It is free on the cs106a class website.

Q: can we use the same commands that we learned in Karel in python (eg. for i in range etc..

A1:  Yes! You can use for loops in python!

Q: When does assignment 2 come out?

A1:  Friday!

Q: can you define a string again?

A1:  A sequence of characters surrounded by quotes.

Q: are the 4 types we learned today the only types in python?

A1:  There are lots of types in python.

Q: how do we have to submit the homework?

A1:  There is a submitting assingments handout. You will be able to submit later this week.