Lecture Materials

Class Announcements

Assignment 2 will be released after today's lecture.

Questions & Answers


Q: Where can we find the video recording of the lectures?

A1:  Canvas


Q: Hey CS106 team, would it be possible to have the slides ahead of time to preview the topics? Even if it is an earlier version of slides, it'd be appreciated :D

A1:  Unfortunately they’re usually only ready the morning of the lecture, which is when we post them.


Q: when I close the program, the luggage disappears? or is it on the disk? more locally, does defining a variable in a function only has it available in that function?

A1:  Good question! All the luggage disappears when the program ends, and when a function ends, its luggage disappears too


Q: After “modifying” age, if you called it again, would you get 30 or age+1?

A1:  you'd get 31

A2:  age = 30 age = age + 1 print(age) This will print 31, as age now has the value 31


Q: I haven’t been able to find any handouts for the new info since Karel, is it on the CS106A website?

A1:  There's a Python reader on the CS106A website. The link is in the upper right-hand corner. It's the reference reader for Python.


Q: While “True” just assumes it is always true? Or what is true contigent on?

A1:  live answered


Q: what is the specific function of being ¨tru¨

A1:  live answered


Q: I’m a little confused, what is while “true” as in what specifically is true?

A1:  live answered


Q: Do we need to import a math library for the log function?

A1:  Yes


Q: so the while true: loop just repeats the function over? What are we defining as "true" in the example that Chris just did?

A1:  It’s a value that’s defined in Python - we’ll get there soon!


Q: is Chris holding a doggo?

A1:  live answered

A2:  Yes!

A3:  He is! The dog is freaking out because Chris’ fiance had to take a call and it’s feeling separation anxiety :)


Q: What is a boolean?

A1:  A variable type that is either True or False

A2:  Sometime a logical test is also refered to as a boolean condition (e.g., 1 < 2)


Q: Is an input always a string (even if the user inputs a number)?

A1:  Yes, input always gives you back a string


Q: this would only work once though right?

A1:  Yes, it wouldn’t repeat


Q: If we pass the integer 1 into an if statement does that count as true?

A1:  Yes, but we’ll get there later this quarter


Q: would you not indent as normal with elif?

A1:  You do: the indentation should look like what it does in the slide righ now


Q: If we had use num=float(input(….)) should we have written if num == 0.00 ?

A1:  Python is clever enough to know that 0 and 0.00 are equal.


Q: would if elif elif ... else work?

A1:  Yep, you can have as many elifs as you want.


Q: Can you do this with a float?

A1:  Yep


Q: what about if you want to also consider decimals_

A1:  You would replace the call to int with a call to float


Q: it says integer, only right?

A1:  You would replace the call to int with a call to float


Q: what is elif? didnt catch what chris said

A1:  ‘Else if’


Q: Is elif always written before else? And can we write this code “num === 0 ; num > 0 ; num < 0) after if, elif, or else?

A1:  The order is 1) if 2) as many elifs as you want 3) else


Q: If you wrote two “if” statements and then an else, would that have the same purpose as writing an “if”, and “elsif” and an else?

A1:  Nope, because both those if statements could happen (depending on their conditions)


Q: Is there a way to highlight everything you want to comment out rather than individually typing the # before each line?

A1:  Highlight the text and press Command/Ctrl + forward slash


Q: How is a boolean fundamentally different than simply using an if or a while loop on their own

A1:  if statements and while loops use booleans to represent whether their condition is true or false. while front_is_clear() is a loop, and front_is_clear() is a boolean condition


Q: how do you know whether to use variable or a constant? like for secret_number

A1:  a constant would be if you could fix the value whilst you were progamming (say, it was always 42). When the variable has different values on each run, you should make it a variable.

A2:  constants are known when the program runs and don't change during the run of the program. Here, secret_number is not known/fixed before the program starts, so it's a variable.


Q: Will the random number this program pulls be the same every time?

A1:  Nope - we’re not calling random.seed() first


Q: what does the quotiations inside the brackets in print, mean?

A1:  print("") is printing an empty string (which is nothing). It essentially just writes a blank line.

A2:  It delimits a string (which represents text), which you then print


Q: when is an empty line printed?

A1:  When you call print(“”) or print()


Q: why do we need the empty line ?

A1:  Just to make the output of the program look nicer.


Q: is it necessary to say “guess=int(input(“Enter a new guess: “))”? Can’t we just say “guess=input(“Enter a new guess: “)” without the cast/specifying the variable type? I thought python could immediately detect the type of variable we input

A1:  Nope - if you manually set up a variable by using something like n = 7, it’ll detect it, but input() will always give you back a string.

A2:  input always returns a string, even if the user types a number. So the user typing 5 would lead to input giving you "5"


Q: why did chris print an empty line??

A1:  Just to make the output look nicer


Q: If the first number you enter is -1 would the total then be zero becaue the sentenial is satisfied?

A1:  Yep


Q: what is += again?

A1:  x += 1 is equivalent to x = x + 1. += is an operator that adds a value to a variable and stores it back in the that variable.

A2:  total += new_num is the same as total = total + new_num


Q: So since Chris is using an int() function, if the user gives a real number, does it get rejected?

A1:  live answered

A2:  Yes, the function crashes


Q: why is total=0?

A1:  The sum is 0 before you have any numbers

A2:  that's the initial value of total


Q: what is " +="

A1:  total += new_num is the same as total = toal + new_num


Q: How do you cancel the running program to start a new one?

A1:  Type ctrl + c in the terminal


Q: will float(-1)==int(-1)?

A1:  Yes


Q: wait how do I make a new python file?

A1:  Command/Ctrl N in PyCharm and then select ‘new Python file’

A2:  live answered


Q: we can use decomposition, right?

A1:  Yes, but that’s not quite the only thing we need to do.


Q: could we define new_num outside the loop and then have it replaced every time we enter the loop?

A1:  live answered


Q: What button on your keyboard can you press again in terminal to run the program you had run before

A1:  The up arrow key


Q: whats +=

A1:  total += new_num is the same as total = total + new_num


Q: Isn’t it more convenient to solve the fenceposting problem with an if break statement inside the while loop i.e. if number == -1: break ?

A1:  That’s a different approach, but is also reasonable


Q: Can we just def new_num at first before the while loop? Would that fix the LocalUnbound error?

A1:  You would need to give it an initial value, but yes


Q: why is the total 60 and not 59?

A1:  the sentinel isn’t added to the total


Q: what if you wanted to do the same thing, but instead you wanted the loop to continue until the user input ANY negative number? not just -1

A1:  You’d adjust the while loop condition - try and figure out how!


Q: What exactly does the condition += mean?

A1:  total += num is a python shorthand for: total = total + num


Q: is there a way to do “”” “”” multiple line comment without the “return” appearing and having to delete it?

A1:  I think it’s buried somewhere in PyCharm’s settings!


Q: how come you dont need to ask python to print the original command for the first number

A1:  Can you clarify what you mean?


Q: why is the total case necessary

A1:  It represents the total of all the numbers you’ve already inputted


Q: don’t you need to str(total) at the bottome line of the code?

A1:  Yes! Good catch


Q: Does "break" jump us out of the inner while loop or the outermost while loop?

A1:  Just the innermost one

A2:  innermost loop is what you break out of. So if you have nested loops, you only break out of the innermost one.


Q: can we use break?

A1:  You can, but you don’t need to


Q: does break exit just the loop it is in, or all loops (if there area nested loops)?

A1:  Just the innermost one

A2:  Just the loop it is in.


Q: Is break always indented one from the previous line?

A1:  It was indented in that case because it was inside an if statement - you don’t need to always do it.


Q: If there is no num input within the while loop, would that while loop then become an infinity loop as it will constantly be checking the first num value which is always not equal to -1?

A1:  Yep


Q: so in the ex we are looking at currently, the first command under the while loop is num = int(input(“enter number….). That command requests the user to enter a number, but you never told python to print that request

A1:  input prints the prompt before getting input from the user


Q: when is hw2 being released and when is it due?

A1:  Right after lecture, and it’s due next Friday


Q: How do you make multiple lines in python shift to the left at once (opposite of indent)

A1:  Highlight them and hit shift-tab


Q: Do we have python counterparts to logical operators such as &&, & and |, || ?

A1:  Yes, we'll cover those soon. It's "and" and "or" in python.


Q: can you go through the example for not

A1:  First we check if 2==3 (which is False), and then the ‘not’ inverts it so we have a final result of True


Q: If the answer was True and False would it evaluate to False?

A1:  Yes


Q: What if it was True or False?

A1:  That would be True


Q: When would i get true and true

A1:  Can you clarify what you mean? That happens when the things on either side of the and evaluate to True


Q: its hard trying to thing about what a true and true would cause in the code

A1:  True and True evaluates to True


Q: 4 == 4.0 is this true or false?

A1:  True


Q: expressions and variables are interchangeable?

A1:  The results of expressions can be stored in variables


Q: you know how yesterday we talked about how python sometimes does arithmetic in a way that’s totally whack? like it’ll represent .9 as .89999999? does that pose a problem for boolean logic? how do we fix that?

A1:  Good question! For some numbers, floats are precise enough that == works. Otherwise, we do run into some issues and the easiest way to fix it is to round the float. That said, this isn’t an issue that frequently, practically speaking.


Q: For the food and drink example, would this issue be solved by writing it as not ( food or drinks)?

A1:  Yes. It's a place where the English language is ambiguous, but Python requires us to be precise, for example, by adding parentheses about what should get evaluated first.

A2:  Yep!


Q: could you explain more about void()?

A1:  That’s not a thing in Python, but it means ‘nothing’. In Python, the equivalent idea is called None


Q: so we would not have public or private void () in python?

A1:  No


Q: Why did i just saw it on screen? Sorry I might have missed something

A1:  Might have been a typo! I’ll fix it


Q: why do you state i = 0 first? And could you do while i < 101?

A1:  By convention, we start counting from 0 in computer science. So if we want to count 100 times, we'd count from 0 to 99.


Q: Why don't we get the 'referenced before assignment' error when we say for i in range()?

A1:  The loop defines the variable for you


Q: Will we have enough knowledge after today's class to complete assignment 2?

A1:  Yes


Q: In a For Loop, why do we use i<#, and then add 1 to the count? Couldn't we simplify the function just by using == #?

A1:  By convention we use < # in case you are counting by more than 1 and don't get exactly the number you are trying to count to.


Q: Why don't you need to put str(i) for print?

A1:  If there's no other text you're printing, you can just print a variable x with print(x). We use str(x) when we want to concatenate other text to what we want to print.


Q: so it is not specific to 'i'? We could say: for counter in range()? and it would do the same thing, but define a variable called counter?

A1:  Yep


Q: why don’t we have to define i?

A1:  The loop defines i for us


Q: would …range(0,5,2) do the same?

A1:  You'd get 0, 2, 4 with that range

A2:  Yes, but we’ll get there later


Q: Why do you write for in in range(3) if we’re trying to find the first 100 even numbers? (as opposed to for i in range (100))

A1:  live answered


Q: Can you explain about true and true please

A1:  live answered


Q: I have a question regarding the second way to use parenthesis in the for loop. (_,_,_) since it skips 2, it will not show 2 as an even number, right?

A1:  live answered


Q: :O gotit

A1:  live answered