Lecture Materials

Class Announcements

The diagnostic is next Wednesday. We'll be posting review materials by Wednesday, and if you're expecting OAE accommodations or an alternate time, you should email Juliette by Friday October 2nd at 1pm PT.

Questions & Answers

Q: The lecture notes are not posted yet

A1:  We'll be posting them now.

Q: is it okay for compute_interest to be longer than 10 lines? Im not sure how to shorten the main function bc of variables

A1:  10 lines isn't a hard rule. You just want to try to keep your functions short, but if it really needs to be more than 10 lines that can be okay.

Q: I still don’t get what YEAH hours are?

A1:  They provide early help for the assignment. They are optional, if you might find it useful to get help early on for the assignments.

Q: Hi! I was wondering if it’s okay to pass parameters in our assignment for this week. Thank you!

A1:  Yes. You'll learn about parameters today and you can use them for this assignment.

Q: How do we write pre & post conditions for Python? Or do we just need an overall comment for each function now?

A1:  You just write overall comments for each function now.

Q: Do we still have to decompose for assignmnet 2 if the functions are only two lines of code? The majority of my lines are declaring variables

A1:  If some of your functions are very short, you might not need to decompose them further.

Q: Are we allowed to use for loops with different steps and ranges?

A1:  Yes!

Q: Are the slides for this lecture available on the course website yet?

A1:  They're being posted now. They'll be up soon.

Q: Does it have to be ‘i’ or any variable?

A1:  It can be any variable, but i is used by convention.

Q: When and where can we attend the YEAH! hours? Thank you!

A1:  YEAH for assignment 2 was recorded and is posted. More information is available on the class website.

Q: what exactly does “i” stand for?

A1:  i is for "index".

Q: for the “ for i in range()” function in python, can I use all the parameters in assignment 2? like “range(start, stop, step)”?

A1:  yes, you can use start, stop, and step in your for loops

Q: is there any restrictions for assignment 2?

A1:  Generally, no. But you should only need to use things we cover in class or in the Python reader that's linked to on the CS106A website.

Q: can you manually change the value of i in the body of a for loop?

A1:  There are ways to do it, but it can lead to bad style, so you want to think hard about why you'd want to do this.

Q: in the for loop he just showed, why did it print out numbers in order from 0-9?

A1:  When you do range(10) it actually counts from 0 to 9 (which is 10 total numbers, but Python starts counting from 0.

Q: Can I use nested for loops on assignment 2?

A1:  Yes!

Q: Is there a way to call variables from main to other functions other than global them (which I believe we shouldn't use)

A1:  You can use parameters and returns. We'll talk about that today.

Q: for the Sandcastle problems in assignment 2, should we be decomposing our program into multiple functions or is it ok to write all the code in the main loop?

A1:  It is okay to write all the code in main for the sandcastles.

Q: What constant do we need to enter for the moon_weight sandcastle to make it good style? I completed it without any constants and I did not reference any possible constants in my code.

A1:  Consider if there are any numbers in your code other than 0 or 1 that are meaningful. They would be constants.

Q: Do we have to convert code to strings in order to print it? Also, can we print an int and a string in one line or do we need to convert the int to a string so that both pieces of text are string (or, in any situation, both pieces of code are the same type)?

A1:  You can print an int/float directly without converting it to a string if you are printing it by itself. For example if x is an int, you can just say print(x).

Q: what happens if you input a float like 2.5?

A1:  You'll get an error when you try to convert it to an int.

Q: Can we use global variables and change them in the functions? Is that bad style? But if not, it’s hard to separate functions in compute_interest…

A1:  Global variables are bad style. You should not use them. We'll talk about other ways to pass around information between functions today.

Q: what does the "!=" do in the line locked = prize % 2 != 0

A1:  != means "not equal". It's a logical test.

Q: For the sandcastles problems in assignment 2, do we need to write comments and a summary intro paragraph at the very top of the program below Files: “insert_name_here”

A1:  Yes, you should have a comment for each function, even for sandcastles.

Q: Im a little confused about what I need to complete for the first sandbox question of assignment 2 (saying what existing code does)

A1:  You should write what the function prints out.

A2:  It would be a good idea to come to office hours or LaIR to get more detailed help.

Q: would while door (4 or 5 or 6) work? I realize that it’s much easier to write it a different way, but I’m curious as to whether or not multiple ors work.

A1:  Not sure what you mean by (4 or 5 or 6). You can try out the code you're curious about in Python and see what it does. That's always a good way to see what different code does.

Q: For the random numbers sandcastle can we use a for loop and range to produce random numbers 10 times? I am co fused as to how we use the num_random, num_maximim, and num_minimum in our code.

A1:  A for loop sounds like a great idea! Random.randint takes in two values to use as the lowest number and the highest between which a random number will be chosen.

Q: Where can I find the lecture notes?

A1:  Just posted them on the website unlder the lectures tab for lecture 7!

Q: Wouldn't door 1 be 2.9 if it were not a intger

A1:  Not necessarily.

Q: Are lecture notes just the slideshows? Should I be taking additional notes or just listening during lectures?

A1:  Lecutre notes are just the slide shows. It could be helpful to code along to get some practice.

Q: why does elif door==3: for i in range(door): prize+=1 give 7? what is i here?

A1:  i is the variable in the for loop. So prize starts at 4 and then gets 0, 1, and 2 added to it in the loop, giving you a total value of 7.

Q: Quick question about functions. I was programming and set a variable = function(x). And that function returns a value. But, for some reason, the program assigns both x and variable to that return value. Is that how that works?

A1:  Can you make a post on Ed? It may be easier for us to answer/explain on there! If it is code from your assignment, make it a private post.

Q: Do we have to create programs for all the games in the extension portion in order to get credit or can we just choose one to do?

A1:  You can choose to do as many or as few as you want!

Q: Is Chris's individual OH still missing from the class Calendar?

A1:  Yes but they are listed on the zoom details page!

Q: are the extensions extra credit for this assignment?

A1:  Yes ! Extensions will always be extra credit.

Q: how often do we have section?

A1:  Once a week.

Q: Am I in the right class if I have absolutely no CS experience? Feeling a little overwhelmed right now

A1:  Yes this class is for people who have no CS experience! Come to office hours to chat about this further!

A2:  You are in the righ place. If you're feeling overwhelmed, I'd suggest coming to office hours or lair and ask us to go over material with you. Secion is also a good place to ask questions.

Q: how do you get the user input to be italicized?

A1:  It does not need to be italicized in your programs. We just put it in italics so that you can more easily see what is user input vs what is output from your program.

Q: Can we use these functions with variables for Assignment 2?

A1:  Yes!

Q: does return substitute print or are both needed to print?

A1:  Return does not print. If you want to print out a value, you should also have a print statement.

Q: if 5.0 and 10.2 are constants should we use pass 5.0 and 10.2 into the argument or should we pass a variable that’s more descriptive?

A1:  If you have constants for the values you want to pass, you should use the constants (not the values).

Q: What does mid do in the functuin?

A1:  mid is the name of the variable that stores the return value from the average function.

Q: are we allowed to use parameters in assignment 2?

A1:  Yes!

Q: can you return multiple things

A1:  You can, but we won't do that right now. It involves concepts that we'll see much later in class.

Q: what’s a call?

A1:  When you call a function -- similar to what you did in Karel. When you called a function in Karel there just weren't any parameters.

Q: How does it know to refer/return to mid when average is done?

A1:  Similar to Karel, when you call a function, you come back to where you called the function from when the function is done.

Q: If your function returns an altered version of one of its parameters, does that affect the argument in the original function?

A1:  We'll talk about that later. The short answer for now is that for int and float, if you change the value in the function is does NOT change the original variable passed in.

Q: When would calling a function inside of the same function be useful? Can you do that?

A1:  Yes this can be useful, but you will not need to do this in 106A. Take 106B and you will learn more about this!

Q: When would you not need to use the return statement?

A1:  When you need to send back a value from a function.

A2:  If you do not need to send any information back to the parent function.

Q: returning in main will always end the function right?

A1:  Yes. Returning in the main function will cause the program to end, because you have effectively finished the main function. But in this class, you shouldn't be returning in the main function.

Q: will we learn about UI in 106a?

A1:  Not really. We'll talk a little about web-based apps at the end of class. But we won't be building UIs in Python.

Q: does putting the function before main matter?

A1:  Nope. In python you can define functions in any order.

Q: Can you call num in main?

A1:  Not sure what you mean by call num. You would call functions, but there isn't a function called num in that example.

Q: Can you return more than one value from a function? How do you access the different values which are returned?

A1:  You can, but it involves concepts we won't see until later in class. For now, all your functions should only return one value.

A2:  You can, but we won't do that right now. It involves concepts that we'll see much later in class.

Q: can we put a variable defined in main into the parentheses for a function

A1:  If you define a variable in main, you can pass it as a parameter to any functions that you call after defining that variable.

Q: are paramters technically variables? If so, are parameters stored variables that can be used later??

A1:  Yes, you can think of parameters like variables that get an initial value assigned to them (based on the value passed into the function).

Q: can the sandcastles have all the code put in the main?

A1:  Yep.

Q: when should we have the whole python reader read by?

A1:  No. You should read parts of the reader that correspond to the topics covered in class at each point.

Q: In many slides, the main function is below other defined functions. Is this good style or should main usually be at the top?

A1:  It's up to you if you want to put main at the top or bottom. But it shouldn't be in the middle!

Q: are there any undergraduate courses that you'd recommend to learn the underlying electromechanical ways that computers work, like how a compilier physically uses transistors and stuff?

A1:  Depends how low you want to go into the electronics. CS107E talks about sitting directly on top of hardware. You probably want a class like E40 if you want to get into the physical components.

Q: What is the difference between parameter and variable then?

A1:  A parameter represents information passed into a function. A variable is just any value we want to store, even if it's not being passed to us from somewhere else.

Q: Are we graded on sandcastles?

A1:  Yes

Q: so when does the user put in the m? Like where is the input function?

A1:  Here, we're not asking the use for the value. It's just coded in our program.

Q: In this example, how does the system know how to deal with a float or an int? As in, we are not particularly telling the function, what type of parameter it shall receive. Is that okay? And how?

A1:  Python infers the type of a variable depending on the value in it. You don't explicitly specify the types as you might do in other languages.

Q: How does a computer program decide how many decimal places to include?

A1:  It uses as any many decimal places as it has available in the number.

Q: how far into the python reader should we read?

A1:  You should read the sections that correspond to topics we've covered in class so far. If you look at the list of topics in the reader, that would be down to "input()" for now. But some sections of the reader will have more detail than we've covered so far and we'll revisit those topics in the future.

Q: So do you not have to define meters as a variable before using it in the function def? parameter names don’t need to be defined?

A1:  Parameters are defined when you write the function that is going to take in those parameter values.

Q: Can we definite a function to a var? Ex. var = def … If so, when would we need that?

A1:  You can, but we won't talk about that until near the end of the class.

Q: So when exactly do I talk about arguments and when about parameters? (Are parameters inside the author function and arguments the input functions on the caller side?) Why do they change names?

A1:  "Parameter" is used to describe what the function is expecting. "Argument" is used to describe what are the values passed into the function. But sometimes those terms get used interchangeably.

Q: can you return nothing?

A1:  Yes!

Q: in the meters_to_cm example, why didn’t we do int(meters), since 5.2 was just a value assigned to meters? I’ll appreciate your help.

A1:  If we said int(meters), we'd be truncating the value to an integer, which we don't want to do.

Q: for this problem, could we just write in main, print(meters_to_cm(5.2)) or do you need to use the result variable

A1:  You can write print(meters_to_cm(5.2)) in main.

Q: Where’s the meters variable defined for this example?

A1:  meters is a parameter. It's not a new variable. It is the name for the parameter that is passed into the function.

Q: Can one function return multiple things?

A1:  It can, but that involves concepts that won't be covered until later in the class. For now, all your functions should only return one value.

Q: in this meters example, would the 5.2 number be changed by the user?

A1:  No. It is only changed by the author of the program.

Q: Can you use variables from the main in a function? If so, can you change the values of those variables in the function?

A1:  Yes you can pass variables from main as parameters to other functions. We will talk very soon about how those variables change when they are passed as parameters.

Q: who puts the 5.2 there?

A1:  The program author.

Q: If we do it by hand first, are we allowed to use PyCharm to check our trace?

A1:  You can check your trace in PyCharm after you've done it by hand.

Q: so what is the difference between input and parameter?

A1:  Input comes directly from the user. A parameter is used to pass information between functions in a program -- it doesn't ask the user for information.

Q: Can we reuse functions we've already written? Across different assignments I mean

A1:  Yes. You will have to copy them into files for the new assignment. And add a comment letting us know that you did this.

Q: does the def meters_to_cm(meters): function go above the main function/comment?

A1:  You can define functions in any order in python.

Q: What are good practices for naming parameters?

A1:  Something that describes that the value will be. Avoid generic names because it will make your debugging more difficult.

Q: in the meters_to_cm example, why didn’t we do int(meters), since 5.2 was just a value assigned to meters? I’ll appreciate your help.

A1:  We want meters to be a float not an int. Since the program author wrote in 5.2, python was able to determine the type to use for that variable. When we ask for input from a user, we need to convert it to an int or a float because the input function returns a string.

Q: can we have a fucntion optionally accept paramaters?

A1:  Yes! But you will not need to worry about this for now. We will discuss this much later in the course.

Q: Can we return multiple pieces of data from a single function?

A1:  You can, but we won't do that right now. It involves concepts that we'll see much later in class.

Q: should we be using print or return for this weeks homework?

A1:  You should definitely be using print. You can optionally use return.

Q: is the only difference between input and parameters that input is used in function definitions and parameters are used in the main function?

A1:  parameters are used to pass information between functions. input is used to ask the user for information.

Q: Any advice on how to decompose the compute interest or khansole academy program? Right now, I just have one block of code for each function.

A1:  Now that you know about parameters, you may be able to split these into multiple functions that each sovle a different part of the problem.

Q: Is it important to define functions before main? DOes the order matter for style?

A1:  Try to put functions into a logical order. I like to have main first and then define functions in the order in which they are called in main. Alternatively you could put main at the bottom and put the helper functions above. You can also talk to your section leader about what will be easiest for them to understand your code.

Q: when computer finds a ‘return’ in the function, does it stop reading what follows ?

A1:  yes!

Q: What is the difference between a caller and author? Is a caller someone who looks directly at author code and a user just looks at terminal?

A1:  A caller is a person who calls a function. The author is a person who writes the code for the function. The user is the person using the program (who may provide input).

Q: For a nest loop, does return exit out of both or just the loop it is in?

A1:  Return exits out of the entire function. So it would break out of both loops and go back to the caller function.

Q: Does it need to be indented an extra time if there’s no else statement for return?

A1:  No sure what you mean here. Could you provide an example of code?

Q: for this example larger = max(num1, num2) can we make the helper function put more than just 2 values? ex: larger(num1, num2, num3, num4)

A1:  You can pass more than two numbers to the max function.

A2:  You can have multiple parameters defined for a function.

Q: Can you have more than 2 parameters?

A1:  Yes!

Q: so in this parameter, the program asks the user for the input? Or how does it know the two inputs are (1,5)?

A1:  The values 1 and 5 are just coded in the program. We're not asking the user for any input.

Q: what’s the point of functions with parameters when the author of the function can only input one value? why would we just need to use 5.2?

A1:  This is just an example to show how parameter passing to a function works. In any programs, the values you pass to a function may not be hardcoded, but the parameter passing mechanism would work the same way.

Q: Does max help you dictate the range?

A1:  max is just computing which number is larger. It doesn't say anything about the range.

Q: What is the purpose of using larger = under def main in the last example?

A1:  You are assigning the value returned by the function max to the variable "larger"

Q: can parameter be defined as the variabe that the function accepts?

A1:  A parameter is kind of like that. The parameter is just a name to be able to refer to the value passed into the function (similar to a variable).

Q: When writing arithmetic expressions, are we supposed to put a space between the number and arithmetic sign (ex. 1 + 2 or 1+2)?

A1:  yes that is good style!

Q: when we worked on Karel, main function was always on top and helper functions were on the bottom. why isn’t python the same case?

A1:  Order of the functions did not matter in Karel nor does it matter here.

Q: Can we use lists in our optional extension?

A1:  Not for assignment 2. You should only use concepts we'ver covered in the class by the time the assignment is due.

Q: can a function return multiple things?

A1:  You can, but we won't do that right now. It involves concepts that we'll see much later in class.

Q: Are we still targeting ten lines or less per function?

A1:  In general, but it's not a hard rule. As programs get more complex, your functions will probably get a bit larger.

Q: does this mean that when we decomposed functions for the Karel assignment, main was the author and everything between main and the smallest function was a caller?

A1:  The caller is used to refer to the function that called another function. The author is the person who wrote the code for the function.

Q: where can we find all these example codes that chris is showing us?

A1:  They will be posted after class.

Q: What does the pass function do?

A1:  It does nothing. It's essentially a placeholder. It's a way we can specificy a command when Python is expecting one, but the command does nothing.

Q: are we expected to use parameters for assignment 2? i completed it already and I want to know whether i have to go back

A1:  Yes, there are places where it would be good style to use parameters in assignment 2.

Q: what does *= and += mean?

A1:  It is shorthand. x *= 4 is the same as x = x*4. Similarly, x += 3 is the same as x = x +3.

Q: Can you call functions from different files in the same folder?

A1:  Yes, but we will talk about that later in the class. For now, you'd need to include the functions you want to call in the same file.

Q: Does there need to be a line of space between the 2 return functions?

A1:  No, but return num2 needs to be at the same level of indentation as the if. Otherwise the function will return nothing if num2 > num1.

Q: can we have variables called sum and min even though those are keywords, or is that bad practice

A1:  Cannot use those as variables because they are keywords.

Q: how do we decompose our code and define different things when we have variables, when I tried this I would get a local variable reference error

A1:  Try passing them as parameters! Bring your code to LaIR or office hours to get more specific help.

Q: How do we get our pycharm to not mark everything as errors?

A1:  You can try: https://www.jetbrains.com/help/pycharm/disabling-and-enabling-inspections.html

Q: how do parameter functions work in multiple situations if they don’t ask for an input from the user?

A1:  live answered

Q: are we allow to return more than 1 variable when calling a function for assignment 2?

A1:  No.

Q: For our functions, if we have parameters and use return, should we comment what each of those are?

A1:  Yes please do!

Q: Are we allowed to use functions with paramaters and returns in Assignment 2?

A1:  Yes!

Q: Why wouldn’t he just do for i in range(MAX_SPACES, 0, -1)? Is that bad style?

A1:  He could also do that. That is not bad style.

Q: is print used to pass info between function & user while return is used to pass info between functions? thanks!

A1:  Yes you can think of it that way!

Q: I still don’t really get for loop and i. so does i always start at 0?

A1:  Yes, i always starts at 0. Come to office hours or LaIR to chat more about this!

Q: So the more helper functions I build on top of each other (e.g. now zigzag), in what chronological order would I write them? The lower-level my functions are, the lower they appear in the code?

A1:  live answered

Q: can we use info from this lecture for our assignment 2

A1:  yes please do!

Q: what info can we use for assignment? is it only up to this lecture?

A1:  You can use everything you have learned so far including this lecture !