Lecture Materials

Class Announcements

Sections start today!


Questions & Answers


Q: Do we need to add comments for the 6 warm-ups? The code for those seems pretty short and self-explanatory

A1:  It is good practice ! But we aren’t going to grade you on those. :)


Q: Which questions are we being graded for comments?

A1:  7, 8, and 9


Q: Can you talk about how to turn in homework please.

A1:  Yes! There is a link at the bottom of the assignment handout. Refresh the page.


Q: So if we didn’t watch the uploaded one from Monday missing school we need to go watch that one?

A1:  Yes pls.


Q: How much am I supposed to comment on the questions?

A1:  A few sentences.

A2:  A few sentences. Check out comments in Nick’s lecture examples as a guide.


Q: do we only need to write the precondition? not the post because it can vary sometimes?

A1:  Try to write both if it will be helpful.


Q: What is the breakdown for homework grading? If the code works, but isn't necessarily the most pretty, what is that worth?

A1:  Functionality is weighted more heavily than style. But both parts are taken into account when we grade.


Q: I changed to the wrong section by mistake and changed like the section 2 times in a row, does that cause any problem?

A1:  Nope, that is fine.


Q: where can i find instructions for submitting the homework properly?

A1:  At the bottom of the assignment handout there is a link. Refresh the page if you don’t see it.


Q: how do we submit our psets?

A1:  At the bottom of the assignment handout there is a link. Refresh the page if you don’t see it.


Q: the comments are already provided on question 7. Do we add others?

A1:  Yes please add to them. Especially for the last two functions.


Q: whats the diff between for and while loop? and diff between >>> and return?

A1:  For loops repeat for a specific number of times. While loops repeat until some condition is false but we don’t know the exact number of times it will repeat. We will talk about return later today / in section / on Friday.


Q: if we have comments/notes for ourselves within the hw, should we delete those before we submit?

A1:  Yes pls delete anything that you don’t need your section leader to read to understand/grade your hw.


Q: in the for pixel in image loop example, is pixel defined or is pixel a previously existing thing in python?

A1:  pixel is a name of the variable. It is arbitrary, so you could name it something different like: for any_name in image: but it is common to use the name pixel for that variable when working with images. It makes it easier for other programmers to know what you are talking about.


Q: why is the range always one less than the parameter?

A1:  Because it starts at 0. So there are x amount of numbers between 0 and x-1 inclusive of both 0 and x-1.


Q: Basically, if each question on the hw results in an “all correct,” that means you get full credit on the problem?

A1:  For functionality, yes. We also grade the style of your code.


Q: What does he mean by zero based indexing? Is he just refering to the origin?

A1:  He means that the first x pixel starts at 0 and the first y pixel starts at 0. This is a programming concept where the first value in a list is actually called the 0th value.


Q: why do we need to define a range using width and height to include all pixels in an image? can't we just use for pixel in image, and that includes them all already?

A1:  Yes. You will need to use width and height when you are not doing a for each loop. There will be times when you need access to the x and y coordinates, so you will use nested loops (as nick is showing rn) rather than a for each loop.


Q: Are For loops acceptable for the homework due tonight? I forget when exactly we introduced them.

A1:  No, please do not use for loops in the first homework.


Q: in the homework surver, is there a way to turn on the function that lets the yellow line follow where the program is while running?

A1:  Yes there is a box that says “Highlight lines” below the run button. Make sure that is checked.


Q: does naming a variable in a for loop also define it. For example, in this example y is not previously defined

A1:  Yes it defines it.


Q: I forgot about the lecture from yesterday. Should I watch that first?

A1:  Yes, watch that first.


Q: How is style of code graded? What does this mean?

A1:  In addition to your code solving the problem, we also want to make sure the code is written in a clean way (without doing unnecessary things). Sometimes there are solutions that are more elegant and easier to understand than others and we want you to work on finding these solutions. We also want your code to be easy to follow for other programmers reading it. This is good practice for how you would write code if you worked in industry or on a team project. Your section leader will talk more about this in your first interactive grading session.


Q: does it matter which order you put the for loops in for this example?

A1:  Not for this example, but it some examples it will. It is common practice to just always put y on the outside.


Q: Is SimpleImage() a function? Or is it an arbitrary name?

A1:  Simpelmage() is a specific function.


Q: Is Nick having office hours today and if yes can we ask questions about homework 1?

A1:  Yes and yes.


Q: Why did he do y first in the nested loop, would it be more intuitive to do x first? does using x first still work ?

A1:  It is common practice in coding with images to put y on the outside.


Q: Does y always have to be the outer loop?

A1:  Not always, but pretty much always. This is common practice.


Q: Can you put the for x loop first and get the same result?

A1:  In this example, yes. But it is common practice to put y on the outside.


Q: .width and .height are built in parts of python?

A1:  They are built in parts of the image library you are using for your homework.


Q: Why do you have to specify all the x’s and y’s? Could you use a while loop instead?

A1:  We use a for loop here because we know the exact number of times we want these loops to run.


Q: so image.height and image.width already have pre established values?

A1:  They do for each image.


Q: are we allowed to use for loops in the first hw?

A1:  No. Please avoid for loops in the first hw.


Q: Why is the y so slow to increase?

A1:  When y is on the outside, it goes through all of the x values from x to image.width for that row before increasing to the next row.


Q: Does it matter in the comments if we use the # or the “””

A1:  Yes, please use “”” when writing comments to describe an entire function (right below the def line for that function). For all other comments use the #


Q: is there a way to get the height and width of the image when you first load in the image? print(image.height)?

A1:  You can do: image = SimpleImage(filename) print(image.with) #or image.height Alternatively, you can create a blank image and specify the width and height by doing: height = 200 width = 300 image = SimpleImage.blank(width, height)


Q: Do you have to assign x and y? How do they work?

A1:  When you do a for loop with x or y, that defines the values for you. They take on values from 0 to image.width (or image.height for y vals)


Q: what did that function print (x,y) do?

A1:  It printed out the values of x and y at each pass through the loops. You can use it to help see that y increases more slowly than x.


Q: is defining a variable (x=x+1) allowed for the homework due tonight?

A1:  No. Please don’t use variables to solve homework 1.


Q: Is SimpleImage the image library?

A1:  Yes


Q: In our comments for homework 1, are we basically just writing out in regular sentence form what our code is saying?

A1:  Yes!


Q: For the last example, we would replace a/b with image.get_pixel(), correct? Is there a way to write that in shorthand?

A1:  I missed this in the example, sorry. Can you post an example or a picture of what you mean on Ed?


Q: is defining pixel a = pixel b different than saying pixel b = pixel a?

A1:  Let’s say you have: pixel_a = value_1 pixel_b = value_2 if you say: pixel_a = pixel_b Then both of them would be value_1. If you instead said: pixel_b = pixel_a Then both of them would be value_2


Q: Does a blank image have white or black pixels?

A1:  white


Q: i dont really understand what an out image is. does that just mean you created it?

A1:  Yes! So we are given a filename that we turn into an image. Then we create a blank image. We use the corrdinates/pixel vals of the original image to decide what pixels we put where in the output image (the one that starts blank).


Q: why doesn't return out and then return image on the next line show both images?

A1:  Once you hit a line that says “return” the computer does not read any lines of code below that line. Return is another way of saying “Hey this function is over. Stop reading here”


Q: Under the “code with two images” notes, “# pixel (10, ) in original image” why is it blank after the comma?

A1:  That should be a 0.


Q: so is it right to say an out image is a copy?

A1:  An out image starts as blank. Then we use the corrdinates/pixel vals of the original image to decide what pixels we put where in the output image (the one that starts blank). So it could be a copy or it could look different than the original picture depending on what transformation we want to do.


Q: Can you make it return both out and image so you can compare them?

A1:  Sadly no. But you can return image run it and then return out to double check.


Q: What does “out” do?

A1:  out is a variable name to represent the photo that we are creating. out is the one we are intending to produce as output.


Q: “For the last example, we would replace a/b with image.get_pixel(), correct? Is there a way to write that in shorthand?” This was the example with making 2 of the same pixels and we said ‘b.red, b.green, etc & a.red, a.green, etc’ I’m just wondering if we would just say image.get_pixel() instead of the letter and if there is shorthand for it.

A1:  I see. To set pixel values you would do it like so: a = image.get_pixel(x,y) b = out.get_pixel(x,y) b.red = a.red b.blue = a.blue b.green = a.green You would use the letter.color and there is not shorthand for the above lines of code.


Q: How does Python round non-integer RGB values? (and more generally - what kinds of "bad" RGB values (above 255, etc) can Python handle itself and which will throw an error?)

A1:  Not sure about rounding non-integer RGB values. Check with Nick at office hours or post on Ed and I will look it up. If it goes over 255 I think it actually wraps around, but also not sure about that. I will gather more info.


Q: What’s th return type for the function

A1:  You don’t have to specify the return type in python. But in this case, we are returning an image.


Q: in the for in range lines, do you need to refer to image.height and image.width vs. out.height?

A1:  You can use either if they are the same size. If they are different sizes (as in the aqua example) you will have to be a bit more careful about which ones you use.


Q: The aqua stripe hyperlink doesn’t appear in the lecture notes that have been uploaded to the website - am i the only one?

A1:  https://wopr-service-qbrbcbuzwa-uw.a.run.app/make/image/aqua-stripe


Q: What’s the difference between “for pixel in image:” and the nested loop?

A1:  There is no difference in how they work. You would use the nested loop when you need access to the x,y coordinates.


Q: Is there a link to try out Aqua Stripe on the lecture slides?

A1:  https://wopr-service-qbrbcbuzwa-uw.a.run.app/make/image/aqua-stripe


Q: In problem 2 and problem 7 of homework 1, when I want bit to move right or left it doesn’t happen—even when I put bit.right, bit.left, bit.move, etc. Do you have any idea why this might be?

A1:  Make sure to put the parentheses, so bit.right(), bit.left(), bit.move()


Q: On the lecture notes, it doesn’t have the link to aqua stripe but I can see the link is there on Nick’s copy. Is that intentional?

A1:  https://wopr-service-qbrbcbuzwa-uw.a.run.app/make/image/aqua-stripe


Q: Hello! I'm not seeing the Aqua Stripe exercise linked on the lecture 5 page?

A1:  https://wopr-service-qbrbcbuzwa-uw.a.run.app/make/image/aqua-stripe


Q: is SimpleImage a preexisting function?

A1:  SimpleImage is a library which has many functions including SimpleImage()


Q: how can you test the image width/length if you want to make a drawing but don’t know the dimensions yet?

A1:  You can do: print(image.width) Or you can use that value to make a new image.


Q: can we use for y in range(out.height) instead? since we are changing “out” image?

A1:  In some cases you can use either! But it depends on how many values you want to loop over.


Q: wouldnt it be easier to do this first (make everything dark) then add the aqua stripe though i appreciate the conceptual introduction of how to move things to the right

A1:  You can do it either way. We are doing the aqua stripe first and then making the image dark in this example. Up to you.


Q: Are the lecture exercises graded? I didn’t follow along on the aqua code because I didnt see the link till now

A1:  No they are not graded. Just for practice :)


Q: for the code to create the aqua stripe, why are we not referring to the out image in the nested loop and creating an aqua pixel?

A1:  both image and out have the same height so you can use either inside of range.

A2:  Both image and out have the same height so you can use either inside of range for the y loop. Then we know we are only doing the first 10 x coords (0-9) so we only go to 10.


Q: if the homework says to use 1 while loop can we still use multiple if statements under that while loop?

A1:  Yes


Q: When will this lecture be posted?

A1:  The recording will be posted around 4pm PT. I will post right after this, it just takes a bit to process and upload.


Q: could we have also just said pixel_out = pixel? instead of repeating it for each color?

A1:  No and we will learn why later in the quarter, but basically it has to do with how variables are assigned and changed.


Q: Why are lines 22, 23, 24 nececessary?

A1:  We will go into this in more depth later in the quarter, but it has to do with how variables are assigned and changed in python.


Q: Why is it x + 10 and not x - 10?

A1:  Because the first 10 x coords are the blue stripe, so we need to shift the rest of them to the right by 10.


Q: The 'image' is embedded in the system and in real life we would have to first install(?) the picture, right?

A1:  you will have to have the file that contains the picture in the same directory. You will get practice with this in your homework kind of.


Q: What does pixel = image.get_pixel(x,y) do and when is it needed?

A1:  It grabs the red, green, and blue pixel values at that x, y location in image. You use it whenever you want to transform the pixel or use it in another image.


Q: Would it be bad form to put both of the subtasks under the same for loop?

A1:  It is easier to read this way. You can try to put it in one loop!


Q: what does out.get_pixel do?

A1:  That grabs the pixel at the specified x y location in the blank image.


Q: How do you know the width and height of the image?

A1:  You can do image.width or image.height


Q: Why do you have to move the out image over 10? Why can’t you just put the stripe over the original image?

A1:  In the problem statement we did not want the stripe to cover the image, which is why we shifted everything over.


Q: When i try to resubmit HW 1 it says "failed with status500." What does this mean?

A1:  Email me with a screenshot pls