Lecture Materials

Class Announcements

The diagnostic is next Monday. We'll be posting review materials by Wednesday, and if you're expecting OAE accommodations or an alternate time, you should have received an email from Brahm already.

Questions & Answers


Q: if there’s time, could we quickly go over logical operators? for some reason my brain has a hard time understanding where they fit in…

A1:  We’ll be reviewing them quickly :)


Q: is diagnostic open note?

A1:  Yes


Q: Is it due this Friday or next?

A1:  Assignment 2 is due this Friday

A2:  This coming Friday (April 24)


Q: Will the diagnostic be like an online quiz format like on Canvas?

A1:  No, it will be writing code.


Q: If we have a true variable, whats the difference between: if variable and if variable == true or is there a difference?

A1:  They both achieve the same thing, and the second one is slightly worse style because it’s redundant


Q: how do you clear the terminal?

A1:  Type ‘clear’


Q: what was the “locked” thing in the game show behind door 2

A1:  It was a boolean variable that stored whether or not ‘prize’ was odd


Q: Hey Chris, thank you for the teaching! Would it be possible for you to annotate/write on the slides? I've found this really helpful especially when you're moving faster through a concept :)!

A1:  We'll try to do that where possible, but there have been some issues with tablets for writing annotations recently.


Q: Can a and b be thought of as variables?

A1:  That’s exactly what they are!


Q: What if you want to have multiple outputs?

A1:  What if you want to have multiple outputs?

A2:  For not, you can only return one thing from a funciton - if you’re trying to return multiple values, you should rethink your decomposition

A3:  There is a way to do that in Python but we won't talk about that now becuase it uses concepts you haven't seen.


Q: Can we use the new concepts we learn this week on the homework due this Friday?

A1:  Yep!


Q: How is this different from input()

A1:  Input takes in text from a user - here, the user isn’t supplying the values (the program is just giving them to the average function)


Q: Do we need to descriptively name the parameters, which is what we must do for variables, or is it fine to just use placeholder letters for the parameters?

A1:  You should give them descriptive variable names wherever you can


Q: what if we want to divide by the number of parameters within the parentheses?

A1:  You don’t have immediate access to that number, but remember that you specify how many parameters a function accepts in the def line.


Q: For this average function, you can only fil with two parameters. Will it call an error if you input more than two?

A1:  Yep. Try it out!

A2:  Yes (given what you've seen so far). It's an error if you give a function a different number of parameters than it expects.


Q: Can we use concepts of this class for assignemnt 2?

A1:  Yes


Q: could you explain more on “evaluate”?

A1:  That just means to calculate what a particular expression’s value is


Q: if we already did assignment 2 should we go back and change it to use this? Would doing it without these functions be bad style?

A1:  Compute Interest should use these ideas, and a few other ones could probably benefit from it as well.


Q: is it possible to return some value while simultaneously continuing a function, say store a value in a varible?

A1:  No. When you return a value, the function is done.


Q: Why do you have to use return (couldn’t you just put sum / 2 without the return before it)? In other words, what’s the value of the return function?

A1:  The return statement allows the average function to actually give them sum variable back to the function that calls it


Q: Would you ever put return alone at the end of the function?

A1:  Yes, that's possible.


Q: Does return print the value in the terminal? Or do you need the extra print(mid)?

A1:  Return does not print anything. If you want to print out to the console, you'd need to use print(...)

A2:  live answered


Q: Do you have to input the same type into the function as you return?

A1:  live answered


Q: what’s the main difference between print and return?

A1:  Print(...) prints output to the console. Return is returning a value in your program -- it doesn't print anything to the console.


Q: Do you have to use return? Could you just have calculated the average in a new line under the average function?

A1:  Yes, but you need to return in order to give back the average to the main function.


Q: Why not print mid as a string?

A1:  If you’re not concatenating, the int automatically turns into a string


Q: can you use the return function if you the function you define doesnt require parameters/arguments?

A1:  Yep


Q: Can you define variables in main and have those be your inputs when you call a function? Like if the user had input 5 and 10.2, and we had assigned them to variables and wanted to call the average?

A1:  Yes! We have an example of that later on


Q: why do you have to create mid, how come you cannot just command average(5,10.2)

A1:  You can, but you need to store the return value in some way, so we put it in the mid variable.


Q: So we can use floats, as parameters even though we didn’t mention floats anywhere in the function?

A1:  Yep!


Q: Could you please talk about the way that this “conversation” between the user who is giving the inputs and the programmer who is making these? How should we think about this when writing functions?

A1:  Think about a conversation between functions: essentially, when a function has parameters, we promise that we’ll be supplying it some information. When it returns something, it’s promising it’ll give you back some useful information.


Q: Are parameters inherently a specific type of variable?

A1:  No. Python does not set specific types for parameters.


Q: what is mid?

A1:  mid is a variable that is assigned the value returned by the average function.


Q: is there a way to automatically insert number calculations for a and b values? Or do these always need to be inserted manually?

A1:  Can you clarify what you mean?


Q: what’s the difference between “print” and “return”

A1:  live answered


Q: What if you want to be able to change the number of numbers you're taking an average of? Can python tell how many numbers you put in to the function?

A1:  We’ll learn about a few cool ways of doing that later, but for now, the average function accepts a fixed number of numbers.


Q: and is there a space betweem sum and =

A1:  Yes, but it's not required.


Q: do you always need a return to end a function

A1:  No. Functions without a return don't return a value.

A2:  Nope! If you don’t want to return something, you don’t need to return


Q: Does return give us a float and print a string ?

A1:  Return just returns a value. It doesn't print anything. You call to call the print() function to print things out.

A2:  A float is returned, but when you print it, it gets automatically converted to a string


Q: shouldnt the . be printed also?

A1:  Yep, good catch


Q: Thanks, and why would you do that?

A1:  It’s a nice way of immediatelly ending the function (don’t worry about this for now)


Q: Do we have to define the functions before calling the main function?

A1:  The order doesn’t matter, but you should be consistent


Q: Could the arguments be inputs from the user? (if a function uses parameters, when we call the function can we ask the user to input the arguments?)

A1:  Sure!


Q: does the if (num==5) be set? or it automatically put 5 because the paramenter?

A1:  What do you mean by ‘be set’?


Q: does print_opinion() have to have a number inside?

A1:  Whenever it gets called, we need to supply a parameter, yes


Q: If you have two parameters in the sub function, can you pass one argument and leave the second argument blank?

A1:  Nope, you need to provide as many parameters as the function expects


Q: if we wanted the user to input whatever number they wanted for print opinion would we say something like print_opinion(int(input(“enter a number”))) or can users not input their own parameters?

A1:  They can, and it would look like that!


Q: In the previous example, the variable num no longer exists? So if there is a later function in main that calls num, is num no longer assigned the value (5) that was input earlier?

A1:  Correct!


Q: Could you make your parameters be input from the user?

A1:  Yep!

A2:  You would read values from the user using input() and then pass those values as parameters.


Q: like… the program only works with 5

A1:  Yes, he just hardcoded that


Q: what if we wanted to prompt the user for a number?

A1:  You could do so like we did in friday’s class

A2:  You would use the input function to get a value from the user.


Q: do these functions always need to be before main()?

A1:  No, but you should be consistent


Q: so would just writing meters_to_cm(5.2) withouth defining result produce an error or do noting?

A1:  You’d get back the 520 and throw it away without storing it


Q: Why wouldn’t you do print(str(result))

A1:  Numbers get converted automatically to strings if you’re not concatenating them

A2:  If you're just printing a number (and no other text), you don't have to cast the number to a string.


Q: can you print in the same function where you define a parameter after “return”?

A1:  No, return ends the function


Q: But can the input be a word too or only numbers?

A1:  Parameters can be strings, but that wouldn’t do what you expect it to in this program


Q: So exaclty what is the point of the return?

A1:  So that the meters_to_cm function can give back the number to main


Q: why does it return a float rather than an int

A1:  Because the parameter value passed in was a float.

A2:  Because meters is a float, and multiplying a float by an int gives you a float


Q: Does the called function store the arguments even after the function is finished running?

A1:  No, the variables all vanish when the function ends

A2:  No. Each time you call the function, you get new argument values.


Q: how do you get multiple outputs from one function (ie. in matlab you can do [x,y] = func(inputs)

A1:  For now, you can’t! If that feels necessary you might want to reconsider your decomposition


Q: is there a missing else in this statement?

A1:  You don't need an else, because the function will end as soon as it reaches a return statement.

A2:  live answered


Q: how does it know num1 and num2 are integers? is it assumed to be an integer unless otherwise specified?

A1:  Good variable names :)


Q: why is there no “else” in chris’ example??

A1:  live answered


Q: When would it go to the next return?

A1:  No. A function is done as soon as it reaches a return statement.

A2:  When num1 < num2 and the if statement doesn’t execute


Q: so in this case you wouldnt need an “else” to go with the if

A1:  Yep


Q: why don’t we have to put in an “else” like between the first and second return?

A1:  live answered


Q: so once it reaches return, program is done, correct?

A1:  The *function* is done when you reach return.

A2:  The function is done, yes


Q: So there’s no need for an else?

A1:  Yep


Q: once “return” is excuted, the function is done?

A1:  Yes


Q: can we say that the second return worked like the else in Karel?

A1:  Not quite - the only reason it’s like that is because the first return ends the function


Q: the variable larger only exist in function main?

A1:  Yes.


Q: so return variable gives the parameter not the variable?

A1:  Nope, it returns a variable


Q: he keeps assinging the parameteres but what if we are solving a min, max problem where the user is inputing the numbers

A1:  You can read in numbers from the user using input(...) and then pass those values to the max function.


Q: Do we need to have a line between the two return statements?

A1:  No, you don't need to blank line

A2:  A blank line? No, but it looks nice :)


Q: Just a question about the homework assignment for this week, are we expected to use return functions?

A1:  You can if there are helpful to solving the problem for you.

A2:  You’re welcome to if you’d like, but it depends on your decomposition strategy


Q: What does pass mean?

A1:  It means "do nothing" in Python.

A2:  ‘Do nothing’ - We just put it in so that Python doesn’t complain about a function with no body


Q: What does it mean to print without the enter?

A1:  prints without a "new line" to move to the next line.


Q: do functions take in any type of values? For example, for print_io(), would it take real numbers, negative?

A1:  Yes - it’s the programmer’s responsibility to make sure the parameters are the right type


Q: What does he mean by “not having to put a return after any of them?”

A1:  A return is a newline


Q: but did we define “n”

A1:  n is parameter passed in to print_n_spaces(n)


Q: Why do you need print_no_return as opposed to just print?

A1:  To prevent the newline after each space

A2:  print(...) adds a "new line" (or enter) after it prints the text you give it.


Q: how come when chris runs his code he types “python” instead of “python3”? I tried running “python” instead of “python3” for hw but it ruined my division cause / became integer division, so should I be using python3 or python to run

A1:  You should use python3.

A2:  Force of habit for him! Use python3 :)


Q: will chris’s code be available somewhere to look at? I don’t understand it all too well and would like to look back through it

A1:  live answered


Q: Can you explain what print_return does?

A1:  live answered


Q: If we use the same number of n spaces before printing i or then how come they don’t print on top of each other?

A1:  live answered


Q: I’m sorry I still haven’t understood print_no_return

A1:  live answered