Sept 25th, 2020
Q: how should we submit word files with our assignment? i tried adding them as files on paperless but i couldn't see them as part of my turned-in assignment
A1: Email any worlds that you created to your secton leader
Q: where can we find the lecture slides? i tried looking for them on the cs106a website but couldnt find it..
A1: At cs106.stanfor.edu, check under Lectures menu (at top)
A2: correction to website: cs106a.stanford.edu
Q: Do I have to make an account to acces the code link?
A1: Yes. You need that to access edstem.
Q: how do we login to edstem?
A1: You may need to create an account if you haven't already.
Q: Is assignment 2 available on the 106a website yet?
Q: In the analogy, is the suitcase an object or is the value inside the suitcase an object?
A1: The value inside is an object.
Q: when do you have to use str(age) vs just age
A1: when you want to print age concatenated to another string, you use str(age).
A2: When you want to use the string version of your age rather than the int version.
Q: So unlike functions variables have to be defined before they are used?
Q: when writing age = 31, does it know automatically that it’s an int?
A1: Yes. Python infers the type based on the value. Since 31 doesn't have a decimal point in it, it knows it's an int.
Q: We use the print function to check the value of variable, right?
A1: The print function just prints things out. It's one way to check the value of a variable.
Q: if you use commas instead of + do you still have to use str?
A1: If you use commas, you don't need to use str to convert the variable to a string.
Q: When you change the value of a variable, does it affect the value for the entire program (i.e. lines of code BOTH before AND after the value change) or does it only affect the value for the lines of code after the variable change? Thanks!
A1: Just after you change the value. Think of '=' as doing an *assignment* to a variable. It's not the same thing as '=' in mathematics.
Q: where did HALF_LIFE_CONSTANT get defined in the program?
A1: Somewhere earlier in the program. (It's not on the slide, but it would have been defined earlier in the program.)
Q: what is the link for? sorry
A1: The link is for later in class yo be able to do some live coding along with Chris.
Q: how do you know when to convert variables to strings?
A1: When you want to use the variable as a string, for example to concatante on to other strings to print out a line of text.
Q: for the majority of time, is float (input) the case? instead of int?
A1: Not sure what you mean by the "case". When you use input, you use float or int depending on whether you expect the user to enter a real value or an integer.
Q: When we input a number (because asked in the program), how does it automatically label the number type? Would it be int, float, string…?
A1: The input function gives you a string. You can convert that to other types like int or float, depending on what you think the user will be inputting.
Q: If the input is provided as an integer by the user but you’ve labeled the input as a float, would the program accept the user’s input?
A1: Yes you can convert ints to floats. You cannot as easily convert floats to ints.
A2: Yes. For example, if the user gave you "2" as input, it would be converted to 2.0 when you covert it to a float.
Q: 1<2 is TRUE
A1: live answered
Q: Is this like a IF statement in excel?
A1: Similar concept, but the syntax is different.
Q: Is there a "not" operator? Such as !running, where running is a boolean variable?
A2: Yes. And in Python it's "not". We'll talk about it more later.
Q: if i say x = 3 as an int and y = 3.0 as a float, and I asked dooes x ==y, what would it return?
A2: Checking for equality with floats can be dangerous because (as we talked about last time) floats are not always precise. So tests for equality might not work the way you would expect.
Q: would 2.0 == 2 return True? because theyre different data types, does that cause problems with ==?
A1: Using equality tests with floats can be dangerous because floats are not always precise (as we talked about last time). So if the value 2.0 can be presented exactly, then checking 2.0 == 2 will be true. But if 2.0 isn'r represented exaclty (say, it's represented as 1.999999999999999), then it wouldn't be equal to the int 2. So be careful using == with floats.
Q: Usually, when assignments are released, have we learned everything we need to finish it over the weekend? Or does it also require next week’s content before it is due? Thank you!
A1: Usually, you will have covered everthing you need by the end of the lecture on the day the assignment is released.
A2: For assignment #2 you may need some additional material from Monday for the bigger programs.
Q: Does using elif get you a better grade or can you still use if/else?
A1: You can still use if/else if it makes sense to. elif is for a case when you have multiple else conditions.
Q: If we have multiple else conditions, can we still use if/else?
A1: You can, but elif is sometimes stylistically preferable (for example, if you have multiple distinct cases).
Q: Is the computer reading the first line as string as an input converted to an integer value that is assigned to num?
Q: why are we not converting the input that we are getting from the user from string to float/int in this example?
A1: We are. The conversion is on the same line as the input.
Q: is there any chance we can open up the chat so students can chat with one another? For me, the chat was helping me feel like I am attending each lesson with other students! It also felt more interactive…
A1: Thanks for the feedback!
Q: I thought we had to take 106B before we can take 109...?
A1: It is recommended to take 106b before 109.
Q: Will there be an example of how elif would need to be used as opposed to else and if? Sorry, I’m kind of confused
A1: If you have multiple distinct cases, such as: if (grade > 90): print("A") elif (grade > 80): print("B") else print("C or lower")
Q: why isn’t the second else indented
A1: The else is part of the elif (not the orginal if statement).
Q: What is the difference between if/else and elif?
A1: Nothing. It's just a nice way of making clear that you are cascading a series of if/else statements to handle different cases.
Q: what is elif?
A1: It's the same as "else if". You can find more information at: https://cs.stanford.edu/people/nick/py/python-if.html
Q: What exactly is elif? How does it differ from if?
A1: elif is basically the same as "else if". So you use it at the else part of some other if statement. For more information, see: https://cs.stanford.edu/people/nick/py/python-if.html
Q: Oops I pressed enter to switch lines but anyways is it bad style to write an if statement like: if *condition* blah blah and then if *another condition*? Or should you always do if *condition* blah blah and then else *another condition*
A1: Depends on how you want the program to behave. If both *condition* and *another condition* are true, do you want both of the blah blahs to happen or just one. If both should happen, then you don't use the else.
Q: So can I have as many elif as I want? Can I simply think about them as many further if statements?
A1: elif statements should be mutually exclusive, meaning that they cannot both be true. So you can think of them as further if statements if all of the conditions are mutually exclusive.
Q: in python we can code/define our own conditions?
A1: Yes! We will learn more about how to do that next week!
Q: What is the due date for Assignment 2. It has a due date of April 24th on the website.
A1: Friday October 2nd. I will fix that now.
Q: How long are the sections?
A1: 50 minutes.
Q: can the programmer see the secret_number?
A1: The secret_number is generated randomly when the program runs, so the programmer won't know what it is from the program itself.
Q: can you generate float random number?
A1: Yes. There are functions to do that on the slides from the last lecture.
Q: open the chat! :( :(
A1: It's open.
Q: Are all the comments in the Guess My Program required the same way for our next assignment? Are pre & post conditions still required?
A1: You will need a comment describing each function. Your pre and post conditions will look a little different, but you want to think through these when writing your functions.
Q: so “guess” is defined outside the function but you can edit it in the function?
A1: guess is defined in the function, it is just outside of the while loop. The code here represents one function.
Q: is the suitcase the object?
A1: The suitcase stores the object. The thing in the suitcase is the object.
Q: Why does he have a "!" after "While guess"?
A1: != means "not equals"
Q: Is there a reason we go back to printing secret_number as a string in the last print?
A1: To concatenate the secret_number onto the other text, you need to convert it to a string.
Q: why is the guess variable that is defined inside the function not different than the initial guess variable? I thought that variables are self contained within each function?
A1: guess is defined in the function, it is just outside of the while loop. The code on this slide represents one function.
Q: so if the user never inputs a new guess, will the program stop?
A1: No. The program will just sit there waiting for the user to type a guess.
Q: what does print(“”) do again/what is it for?
A1: It prints a blank line.
Q: If we had the program print “guess” instead of “secret_number” in the last line, would it work also? or is the new guess only defined within the while loop?
A1: You could print guess in the last line.
Q: 1. Why does the “enter new guess” line of code need to be inside the while loop? 2. Is there another way of printing out an empty line other than writing print(“”)?
A1: 1. You want to ask the user for a guess over and over. 2. You can just say print()
Q: Why is there + str(secret_number)? What does that part of the code specifically do?
A1: It converts secret_number to a string, so it can be concatenated to the other text that is being printed.
Q: Is it ok if I wrote assignment 1 with else if statements but not using elif because I didn’t know about it yet?
A1: Yes, that's fine.
Q: why do we want to include print(“”)?
A1: To print a blank line. You can also use print()
Q: So when the “while guess != secret_number” loop gets the random number needed it skips the while loop altogether, yes?
A1: Exactly! When the guess == seret_number, it goes to the print statement.
A2: Yes. When the condition of a while loop is false, you don't do the body of the loop.
Q: if an input is not converted to int, would operators like == and > not work?
Q: how is elif different than if?
A1: elif is like "else if", it's used as the "else" part of an prior "if", but also introduced a new "if" statement.
Q: Can the random integer be greater than 99 (the secret number)?
A1: No. We asked for a random int between 1 and 99.
Q: Can you compare a float to an integer? Like if someone types a number and you convert it to a float and then compare it to an integer as a condition for a while loop
A1: You can, but remember that the representation of floats might not be precise, so testing the equality may be dangerous. 1.999999999999999 is not equal to 2.
Q: When is break needed for while loops? I noticed that in the guess my number program example, break wasn’t needed to exit the while loop. Any clarity on this would be helpful! Thank you.
A1: break is a way to break out in the middle of a loop. If you really need to stop in the middle of a loop, you can use break.
Q: How can we know there is fencepost issue while writing the code itself? Or we can only know that by trial n error?
A1: As you get more familiar with programming you will develop a skill to recognize when there will be fencepost. issues. But it usually happens when something is repeated multiple times and we need it to repeat one extra time even if the condition is not true.
Q: So while python can “pre-read” for functions (ex. you can define helper functions after main), it doesn’t do that with a variable like guess?
Q: how do we start a new project in pycharm?
A1: Go to the File menu, pick "New Project..."
Q: Are there techincal issues from Chris right now, or is it me?
A1: He is giving everyone a chance to work on the problem he was talking about (using the ed platform).
Q: What song is this!!
A1: Take Five by Dave Brubeck
Q: how do i log into ed platform?
A1: You can make an account with your stanford email!
Q: For our programs, are we expected to also put in if statements to handle edge cases? For example in Guess the Number, the program crashes if a float or string was entered instead by the user.
A1: We'll let you know when you need to do error checking for the programs in this class. More generally, it's always good to check.
A2: Ususally thats specified in the program requirements :-). But in general its nice to have a robust program!
Q: Can we uses lists? I can’t think of a way to solve without them
A1: It's possible to dolve without a list. You can keep a running total as you get new values.
Q: when we print total do we not need str(total)?
A1: Yes, you'd need str(total) in this example. You could also write: print("total is", total)
Q: what does total+=num mean?
A1: live answered
Q: Could we solve this problem by creating a constant seed?
A1: That wouldn't be very good stylistically. Chris will show you a better way right now.
Q: But what would happen if you guessed -1 with your first guess?
A1: total would be 0 since you didn't give it any additional values to add
A1: That wouldn't be helpful here since we're not using random numbers in this example.
Q: How come we can print total without converting it to a string first?
A1: It should have been str(total)
Q: Do we need to make total a string before we print it?
Q: so we can use break now?
Q: What does “while true” tell the program?
A1: Theat would loop forever unless we had "break" somewhere in the loop.
A2: It tells the program to continue repeating this while loop until it reaches a break statement.
Q: Are there any restrictions on assignment 2?
A1: You should use material from the class. It does not have the same restrictions as Karel. You can fully use Python now.
Q: can you define what sentinel loops are? thanks
A1: Sentinels are just values you are looking for to potentially break out of a loop (such as the user giving you a -1).
Q: Can we have more thann two conditionals for one line?
A1: There are logical connectives like "and", "or", and "not". We'll talk more about those soon.
Q: would it stop evaluating the if condition when it sees 2==3 first before the “and”
Q: Does python do short-circuiting as well so if the first part of the and statement is false it doesn't check the rest?
Q: Can we put multiple "and" operators in one line
Q: How do break and return differ
A1: break ends a loop. Return ends a function.
Q: what does false and false equal
Q: can we also use continue in our code? or only break?
A1: We're not going to talk about continue. It's not really actually used much and can lead to bad style in early programming. So we're not going to talk about it.
Q: i thought you evaluated the not first though
A1: That's correct, but the second part of an "and" statement is only evaluated after the first part of an "and". So the "not" in this example doesn't get evaluated until you first evaluate the first part of the "and"
Q: Why would you want to asign a variable to true or fale? Also instead of doing x = 4.0<5.0, why can’t we just write x=True?
A1: You can write x = True. But there are times when the value of x will change depending on the value of other variables.
Q: what does public and void mean?
A1: That was a typo in the slide. Looks like a little Java snuck into lecture :-).
Q: Can we use dictionaries?
A1: Not yet!
Q: What are advantages of using built in range() for loop vs while loop?
A1: You use for loops with range() when you want to count some number of times. You use while loops when you don't know how many times you may go through the loop.
Q: Is i an int, str or both? Would you need to say print (str(i * 2))?
A1: i is an int here. You can just print an int by itself using: print(i) We only convert it to a string when we want to concatenate the value of i onto other text to print out.
Q: Does i work in while loops as well?
A1: Not the same way as in a for loop. But you can use variables in while loops.
Q: Does i always start at 0 or can we set i to start at a diffrent number?
A1: Can set it to start at different values with different versions of range(). We'll talk more about that later.
A2: We will get into this on monday, but there are options :). Default is 0
Q: How do we write pre/post-conditions for Python programs not in Karel?
A1: pre/post conditions are mostly in Karel. In Python, we write more general comments descirbing what functions do.
Q: are we capable of starting on assignment 2 this weekend?
Q: can you please go over all the true/false uses? I’m confused on how those can be used with the loops!
A1: your main tools are the comparison operators (<, >=, ==)
Q: can you go over the operators again
A1: Check out operators "if and comparisons" in the Python reader: https://cs.stanford.edu/people/nick/py/python-if.html
Q: Why do you set the total as zero at the beginning
A1: it needs an initial value. its a running sum
Q: which takes precedence, “and” or “or”?
A1: it goes left to right when there is a tie :)
Q: so if we ask it to do someting 3 times, it does it 4 because 0 counts as one?
A1: It will only repeat 3 times. i=0, i=1, i=2 and then it will stop because i is no longer < 3
Q: what does the i+=1 in the for loop to 100 example mean?
A1: It adds one to the value of i and stores the value back in i. Essentially it increments i by 1.
Q: Should we be putting pre and post conditions in the comments for the main method, or just a description of what the program does?
A1: not necessary for main
Q: For the GuessMyNumber program, what was the purpose of the print(“ “) # an empty line line?
A1: Yes, it prints a blank line.
Q: So how come on Wed die1 was not in the loop but num1 does ?
A1: It's just a matter of where you want to define/use the variable as its needed in the program.
Q: How are your days going?
A1: live answered
Q: Does i start counting from 0? If so, shouldn’t we specify one less for the number of times we want somehting to happen?
A1: it starts at 0, it ends at max minus 1!
Q: def main(): x = 0 for i in range(100): print(str(x)) x = x + 2
A1: thats good too!
Q: Can we conbine multiple and/or in a conditions? Like maybe “If condition1 and condition2 or condition3”
Q: So Python supports integers and floating numbers does it support complex numbers?
A1: not natively — stuff that comes with python3! But there is a python “library” for everything that you can download, and certainly for complex numbers
Q: when do we want to set things to strings?
A1: you can’t add strings to numbers, you can only use the “+” operator with two strings
Q: when are not and/or used
A1: In conditions! So in if statements and while loops!
Q: what if you wanted to do the first three odd numbers how would you that?
A1: Good question. you could print(i * 2 + 1) inside the loop
Q: Do Chris have individual OH?
A1: yes! on thursday
A2: Yes check the website for the zoom link!
Q: for i in range(0, 6, 2) counts i like this : 0,2,4 but not 6?
Q: what’s the precendence of booleans?
A1: comparison operators go first. If there is a tie, it goes left to right
A2: Check out: https://cs.stanford.edu/people/nick/py/python-boolean.html