Sept 30th, 2020
Q: Is 106A counted as one of the 106X courses?
A1: in what context?
Q: is a remix of the song "I got a feeling " or is this actually the original ?
Q: Section Leaders Applications
A1: You have to take 106B (or currenlty be in 106B) to apply to section lead. But once you take 106b, you should definitely apply !!!!
Q: What’s this song?
A1: live answered
Q: Is the default input type always a string?
Q: Where’s the link to the spotify playlist that you use?
A1: Playlist is called History of Sounds on spotify!
Q: If we assign a variable x a numerical value without asking for input does that default to a string type as well?
A1: Nope. That variable x would be type int or a float depending on if the number has decimal places.
Q: What does “tracing” mean in Assignment 2? Are we just supposed to talk through what would happen conceptually without running the program itself?
Q: How can we prepare for the diagnostic?
A1: Review lecture slides, your assignments, and go over all of the section problems, especially the ones that you did not get to in section!
A2: Going over section problems and practicing programming in general. We'll also give you a sample diagnostic before the real exam, so you should definitely do that.
Q: So this is only for us, it won’t count as a grade?
A1: It counts as part of your grade too, but an important part of the diagnostic is for you to gauge your own understanding.
Q: I’m struggling with compute_interest, how can I get “unstuck”?
A1: You can go to Lair or office hours to get help.
A2: Try going to LaIR! It is open tonight from 5-9pm PT and tomorrow from 9am-11am PT.
Q: is the diagnostic next Wed or a week after?
A1: Wednesday October 7th!
Q: I’m confused what tracing means in Assn. 2?
A1: It means running through the program by hand (as if you were the computer) to understand what it's doing and determining what the program will output.
Q: Is Karel programming on the assessment or only Python?
A1: Karel programming will be on the diagnostic as well as python.
Q: How do you get the user to input the parameters for a function?
A1: You can have the user give you values using the input() function, and then use those values are parameters to functions.
Q: what happens if you enter more arguments than there are in the parameters?
A1: You will get an error.
Q: does it matter putting the function before or after the main function?
A1: The order in which you define your functions does not matter in python.
Q: do arguments and parameter need to have same variable name?
A1: No they do not! Sometimes it is good style to give them the same name.
Q: Is the exam open note? Timed? Can we run our code? I’m lost
A1: It is open note. It is timed. We'll talk more about it on Friday as well.
Q: How many parameters can we have per function?
A1: As many as you want.
Q: Can we make our variables ‘global’ within functions for assignment 2 or nah?
A1: You should not use global variables (other than CONSTANTS)
Q: So we can have global variables? Or only global constants
A1: Only global constants.
Q: Is it poor style to use the same names for corresponding parameters and arguments?
A1: No. That is often fine.
Q: Is there a reason why global variables are considered bad?
A1: Yes. You don't know when (or by what function) global variab;es are modified, so it creates dependencies between functions in your program. It makes it difficult to write larger programs.
Q: Are constants declared outside of main?
A1: Yes. They are declared outside main at the top of your program.
Q: should we be using functions on part 1 of assignment 2 or can we do everything in main?
A1: You can do everything in main unless you find decomposing would be helpful.
Q: So just to clarify, parameters are used so that variables can be passed between functions?
A1: Yes. It also allows the same function to operate of different values.
Q: If we’re doing a for loop, do we always use i?
A1: You can use any letter, but i is convention.
Q: are we limited to two arguments/parameters inside a single bracket, or can we do more?
A1: You can use as many parameters as you want in a function.
A2: You can put more than 2 arguments/parameters.
Q: where/when was n assigned 0?
A1: When the factorial function was first called, it was called as: factorial(0). The 0 for the argument was what set n to 0.
A2: i starts at 0 the first time the loop iterates. So the first call to factorial took in i when i=0.
Q: What does *= mean?
A1: x *= i is the same as x = x * i.
Q: can we use exit() on compute_interest if our end date is before our start date or is there a better way to do that?
A1: You should not use exit(). You program should complete the main program to finish.
Q: What does “return result” do?
A1: This send back the value of result to the parent function.
Q: do we need quotes or string around (i, factorial i)?
A1: No. This is an example of printing multiple values in a print() that we talked about a few days ago.
Q: If I want to print a period directly next to a number, do I have to convert it into a string?
A1: Yes! Or you can do print(num, “.”)
Q: should we be using return on assignment 2?
A1: You do not need to. But you can!
Q: how does factorial recieve n? I do not see it defined explicitly
A1: n is the name of the parameter in the factorial function. So when factorial is call, whatever value is passed as a parameter is set to n.
Q: why are the i values different in the function call and function definition? thanks!
A1: The variable i in main() is a different variable than the variable i in the factorial() function.
Q: on my random numbers file, it keeps saying invalid syntax for the "if _ _name_ _ ..." statement, can you please tell me why ?
A1: This would be a good opportunity to go to LaIR/office hours. They will be able to help you with the problem. In the mean time I would check your indentation and make sure that there is code inside each function defintion.
Q: theoretically could you put factorial(7) and then n would be “assigned” 7 in the factorial function?
Q: when you return result, why do you not write result as a parameter instead of i?
A1: We need to return the value because if we just changed the parameter, it would not be changed outside the factorial function. We'll talk about that more soon.
Q: why do we need to type “return result” if it will automatically go into the main when main initiates print(i, factorial i) ?
A1: You need return to say what value the factorial function is giving back to who ever called it (which is the main function in this case).
Q: why is the comma not printed?
A1: This is an example of printing multiple values in a print() that we talked about a few days ago. It takes in multiple values separated by commas and prints those values to the screen.
Q: What happens to the “card” when it goes away? Does computer auto delete it? Do we have to delete it ourselves so we don’t run out of memory?
A1: The computer automatically deletes it.
Q: how is “i” determined in the factorial function?
A1: i is a variable inside the factorial function that is the counter in the for loop.
Q: A for loop that has for i in range(1,1) just has i = 1 right? And for loops are not inclusive correct?
A1: Try it in python and see what happens. You should always feel free to just test things like this yourself to see what they do.
Q: Are we meant to decompose compute_interest? If I get the user info in one function, how would I access those variables, without making them global, in another function that let’s say projects future returns?
A1: Yes, you shoudl decompose. You might need separate functions to get month and year since you can only return one value from a function.
Q: Is it bad style to have multiple functions on programs like compute_interest? I know we are allowed to have as many functions as we need but it looks cumbersome since each of my functions takes so many parameters. Should i instead keep it within main?
A1: No it's not bad style to have many functions. It's better style to decompose well.
Q: is parameter passing similar to decomposing (on a conceptual level)?
A1: Yes they are related! You pass parameters into functions when you decompose.
Q: should for i in range (1, 2) repeat twice?
A1: Try it out in python!
Q: Can we use the info from MW’s lectures for assignment 2?
Q: wht does doctest mean?
A1: Doctest is just the name for the testing framework in Python that Chris is talking about right now.
Q: what does -m and -v mean
A1: They are part of the command to run doctests. The -m is referring to a module that it being used and -v means "verbose", so your doctest produces an explanation of whether it was successful or not.
Q: How do you doctest on Mac?
A1: python3 -m doctest fact.py -v
Q: when is the factorial actually calculated in this function? is it in the *= part?
A1: Yes, the loop (with *= in it) computes factorial.
A2: The entire for loop calculates the factorial.
Q: how do I doctest on a mac again?
A1: python3 instead of py in the line on the slide
Q: Do you leave the doctest code in there or delete it once you make sure it works? Thanks for all your hard work answering qs!
A1: You leave it there. It allows you to keep testing if you make changes later. But yuo definitely want to keep them in.
A2: You leave them in even after you get it working! That way people who look at your code can see exmamples of input and output.
Q: can you explain for i in range (x, y) x is starting number, and what is y? final number? why does it say n+1?
A1: You can think of y as a final number, but i will never actually equal y. i starts at x and goes up to, but not including, y.
Q: what should we do if we feel really behind?
A1: You can review the lecture videos on canvas and the slides posted on the website. Also, come to office hours or Lair to ask questions.
A2: Ask conceptual questions at LaIR, work on the extra section problems, come to individual office hours to get your questions answered! Reach out to Juliette via email if you want more tips!
Q: should we be adding doctests to all our function comments on assignments and tests from now on?
A1: It would be helpful. In some assignments we'll provide doctests and in some cases we might require you to write some. But it's always a good idead to add your own to make sure your functions work.
Q: why is the add_five(x) function definition above def_main? is it beacue the variable add_five can’t be referenced before being defined? thanks!
A1: You can define your functions in any order in python. This would also work if you defined add_five below main.
Q: Is there a difference between typing “Python3…” in the terminal and just pressing the green play button to run the program?
A1: For most purposes, they are the same.
Q: I’m a little but confused about Lair, is it like office hours where we can just show up or do we need to schedule a time.
A1: You can just show up. You will need to signup to get into the queue (line) to get help. But you don't schedule a specific time. It's just a line you get in and then get help when it's your turn.
A2: You have to sign up. Sign-ups open 30 minutes before LaIR opens.
Q: why dont we specify the “type” for the parameters or variables?
A1: Python isn't "typed" in the same way other languages are. So, it's just the way the language is defined. Python infers the types of the values.
Q: I would like to attend Lair but im a bit confused, so is it a open zoom link where you can just join whenever and there will always be at least one person there to help you?
A1: You sign up in a queue and a section leader will join your zoom link when it is your turn to get help. It is one on one help.
Q: can we also put “return x += 5” for the def add_five(x)?
A1: No. That's actually not valid syntax.
Q: Is it considered bad form to use the same variable names for variables in sub functions and main functions
A1: That can be fine.
Q: should we create a new variable to assign to the new value generated by the function add_five(x)? or does simply x work
A1: You can do either one. It depends on if you want to save the original value of x or not.
Q: I thought that the return function was optional. Clearly in this case it isn’t, so am I misremembering this or is return function sometimes optional?
A1: Return is optional! You use it when you want to give information back to the parent function. If you want to give information back to the parent function, then return is not option.
Q: If you have the user input a value with input(), then store that value as an int inside a variable, every time you use that variable will it prompt the user to re-input a value?
A1: No. You will only get a value from the user when you call input the first time.
Q: are all these example codes available somewhere (other than in the lecture slides that are posted on the website)?
A1: Yes next to the lecture slides there is a way to download the lecture code!
Q: Why isn’t def add_five(x) expressed def add_five(3)? Like, why isn’t the parameter listed in the new function?
A1: add_five has a parameter x so it could be called with different values for the parameter. It wouldn't make sense to define the function with a value rather than a parameter.
Q: does doctest work for all programming languages or is it a feature of Python only?
A1: Just Python. Other languages may (or may not) have other testing frameworks.
Q: is Chris writing using a pencil or the mouse? its cool!
A1: It is called Ink2go I think. and I think he is using a pencil.
Q: Is "helper function" the same as "author function"?
A1: Helper function is just a way to refer to a function that helps you compute something. It has to be authored by the programmer.
Q: Can functions return multiple values? For example, can we have return (10, 15)?
A1: For now, functions can only return one value.
Q: Any advice on how to use parameters in compute_interest?
A1: Yes! It is not possible to return multiple values, so can you think of a way to decompose a function that does not need to give any information back to main?
Q: What can we put after “return”? Is it always only a variable/data structure? can we also put an expression? like “return (x + 10)”?
A1: You can put an expression!
Q: I just tried running this in terminal, but I am getting “x is none” as my output - not sure why?
A1: What did you try running?
Q: When defining functions, do you always need to use return?
A1: Nope. You only need to use return when you want to give information back to the parent function.
Q: So in order to make things more clear for ourselves and readers, should we make variables all different names?
A1: Sometimes it is helpful to have variables in different functions to have different names. If it is more clear to you to have them all have different names, that is also fine.
Q: what does it do if we define function inside body of another function?
A1: You don't want to do that in this class. If you have functions inside the body of other functions, it means those "inner" functions can only be called inside the function in which they are defined.
Q: For using parameters for compute interest assignment, would we decompose so that we have a separate function just for computing interest and return the new balance back to the main?
A1: Yes, that's a reasonable decomposition.
Q: If you use a function with parameters, is it always necessary to use return? In what cases would you not?
A1: You would use return when you want to give information from your function back to the parent function. If you do not need to give any information back, you do not need to use the return.
Q: This is what I tried running: def add_five(x): x += 5 def main(): x = 3 x = add_five(x) print("x = " + str(x))
A1: add_five() does not return anything. So, python defaults to returning nothing. So, you set x = nothing.
Q: Can you overload methods in Python? Since the parameters taken in don't have a specific type as seen in other languages like Java, how would this work/differ if it does exist?
A1: You cannot overload functions in Python.
Q: If your compuete_interest.py runs the correct program but does not use functions and parameters, would you recommmend rewriting to include functions/parameters?
A1: Yes. You should decompose the program using functions and parameters.
Q: How do we use Python to run and show things on a user interface other than the terminal (like a website or app)? What other tools would we need?
A1: We'll talk a bit about images and graphics in future classes, but we won't get into building user interfaces in Python.
Q: Do you only add Doc Tests is the function isn't simple?
A1: You can add Doc Tests to any function to ensure that it is working properly!
Q: any advice for putting those initial five questions (start year, month, end year, month, initial balance) in compute_interest into a separate function rather than main?
A1: You might need separate functions for getting month and year.
Q: what is the rule for what day the year starts and which month of that year gets 31 days?
A1: Chris is writing days_in_month right now. For first_day_of_year, the code is posted on the class website.
Q: What do all the >>> characters do in the code?
A1: They create a doctest.
Q: Do we need to use doctests for assignment 2?
A1: No, but you can use them!
Q: can we/do we need to do doctests in assignment 2 (especially in the compute_interest problem)?
A1: You do not need to, but you can!
Q: For the compute interest assignment, in order to reset the acount balance, month number, and year number so the user can input a new interest rate, do we need to just create two different variables for each component (one that stays the same and one that changes for each interest rate and then changes back to the unchanged variable)?
A1: You might need to. It depends on how you structure your program.
Q: what are the office hours tomorrow?
A1: See the office hours web page: http://web.stanford.edu/class/cs106a/oh.html
Q: Are doctests information we search up online like here it number of days in a certain year and month?
A1: No. doctest is a testing framework to be able to test individual functions in python.
Q: if you execute print_month(first_day_of_year(year), 1, year) instead of assigning first_day_of_year(year) to first_day, would that be bad style?
A1: That would be fine in this case. In general, it depends on whether it's clear in your program what's going on.
Q: How would you access year in main if it was defined in another function?
A1: year is defined in main and then passed into print_month.
Q: i have one function that gets user input for compute interest, is this bad? should i separate month and year
A1: Think about whether you can return month and year from the function. Since you can only return one value from a function, you might need two functions.
Q: Is there a place where we can access calendar.py to study it?
A1: Yes! there is a link to download lecutre code on the lecture8 page
Q: Why he use += rather than =?
A1: He wants to append a space to the end of what he already has. If he just set it = ‘ ‘ then it wouldn’t count all of the spaces before that.
A2: '+= with strings appends onto an existing string (rather than just assigning over the existing string).
Q: is this our next homework problem?
Q: Why are the days four spaces apart?
A1: Chris decided that is how he wants to style the calendar.
Q: what’s format_number?
A1: It creates a string which is always 4 characters long and includes the number. Since single digit numbers need 3 spaces around them, but two digit numbers only need 2 spaces.
Q: dont we need a new line after each row of the week
A1: Yes. The program isn't done yet.
Q: Would our assessments problems be of similar difficulty as this calendar problem?
A1: The calendar program is a bit more involved that a question on the diagnostic. We'll give you a sample diagnostic, so you can get a sense of the problem difficulty.
Q: what does format_number() mean?
A1: It creates a string which is always 4 characters long and includes the number. Since single digit numbers need 3 spaces around them, but two digit numbers only need 2 spaces.
Q: How does the program know that 2021 starts on a Friday?
A1: It uses the first_day_of_year function.
Q: how does the function know what n_days means?
A1: Live answered
A2: live answered
Q: is current line a variabe that was created? And if so, and it equals one space, why does it not offset the rest of the line?
A1: current_list is a string that starts empty and then has spaces and numbers appended on to it.
Q: is days_in_month a function that's in another function?
A1: No. days_in_month is not inside another function.
Q: I’m getting a TypeError saying NoneType object cannot be interpret as integer
A1: What's the code you're running?
Q: Does first_day_of_year function work for every year or just 2021?
A1: Every year. It uses a formula based on the year.
Q: is first_day a function?
A1: first_day is a vairable. first_day_of_year is a function.
Q: when will we be able to access the sample diagnostic?
Q: if im having a tough time understanding the calendar code and thought process behind it, am i going to find difficulty in completing the diagnostic? i generally feel confused about this new material but im not sure how to catch up
A1: The code for the calendar is posted online (along with the lecture slides). You can review the code and run it yourself to help you get more comfortable with it. If you still have questions, please come to Lair or office hours.
Q: how does the program know what the first day of the year is?
A1: There is a function called first_day_of_year that helps determine what the first day of the year is.
Q: print_month_header(month) n_days = days_in_month(month,year) current_day_of_week = first_day #put blanks at the start of calendar current_line = '' for i in range(first_day): current_line += ' ' print(current_line + "1") for i in range(n_days): day_of_month = i + 1 current_line += format_number(day_of_month) current_day_of_week +=1 if current_day_of_week == 7: current_day_of_week = 0 print(current_line) current_line = '' print(current_line)
A1: Did you define format_number?
Q: How does the program know which doctests to run?
A1: You can right-click on the doctest in PyCharm (which brings up a menu) and pick the option to run the doctest.
Q: should we just think of first_day_of_year as a black box
Q: Is there a neater way to write it other than 4 “or” statements?
A1: In this example, you have 4 different values, so this is about as simplified as you can get.
Q: if this calendar program is confusing should we go to office hours?
A1: Try reviewing it on your own, but then come to office hours/LaIR if you are still confused.
Q: if you want to return a variable you delete pass and replace it with return…?
A1: live answered
Q: Will Chris' code be available for us to look at later?
A1: live answered