If you don’t see your question here, post it on Piazza or come to office hours! See the links in the navigation bar for both of those options.
Do we need to include docstrings in all of our functions?
Yes. For now and evermore!
I’m having trouble with some of the string-method puzzles. Any suggestions?
When using string methods like replace
and split
that take
one or more separate strings as inputs, keep in mind that you can
pass in a string that has more than one character.
When adding new methods to the Rectangle
class, are we allowed
to include the self
parameter in the header, even if the problem
doesn’t explicitly tell us to do so?
Yes! In order for a function to be a method, it must have the
special self
parameter as its first parameter, so you should
make sure to include it in every method that you write.
How do I get the height and the width of an image?
Because we are representing images as 2-D lists of pixels, the height of the image is given by the number of rows in the 2-D list, and the width is given by the number of columns:
height = len(grid) width = len(grid[0])
When I test one of my image-processing functions, I get an error message indicating that an index is out of bounds. What am I doing wrong?
You are trying to access a pixel using a row and/or a column index that is either too big or too small.
For example, for an image that is only 10 pixels wide and 10 pixels tall, trying to set a pixel at row 15 and column 30 would produce an error message.
If you see this error, try to determine why your expressions for the row and/or column indices are incorrect.
It may also help to remember that row and column indices start from zero, just like list indices. For example, a 10 by 10 image does not have pixels at row 10 nor column 10.
How can I implement flip_horiz
?
For this problem, you will again want to start by creating a new
image (you can use create_green_image
for this purpose). Next,
you will go through every pixel in the original image, and decide
where to copy it into the new image. When we flip an image
horizontally, every pixel will stay in the same row, but it will
be put into a new column.
Before you try to write code for this, it may help to draw a table
that maps where pixels go in the flipped image based on their
column in the original image. Suppose w
is the width of the
image. You will end up with a table that looks like this:
column c in original column in flipped ----------------- ----------------- 0 w - 1 1 w - 2 2 w - 3
Given the table above, what is a general formula you could come up with to determine a pixel’s new column in the flipped image?
I’m finding it hard to figure out where the problem is in one of my image-processing functions since the 2-D lists of pixels are so big. Is there anything I can do?
In the ps7image
folder, we’ve included a very small 9x9 image
called rainbow.png
that you can use for debugging:
To do so, you should begin as usual by loading in its pixels:
>>> pixels = load_pixels('rainbow.png')
Then you should pass that 2-D list of pixels into the function you are debugging and store the results. For example:
>>> flip = flip_horiz(pixels)
And because rainbow.png
is so small, you can actually look at the
resulting 2-D list of pixels in the console:
>>> flip result: [[[150, 75, 0], [150, 75, 0], [150, 75, 0], [150, 75, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0]], [[100, 255, 255], [100, 255, 255], ...
We have truncated the result above, but you can find the expected 2-D lists of pixels in the following files:
You can also focus on particular rows or cells of the results by entering the appropriate expression from the console. For example, to see the first row of the 2-D list obtained above, you could enter the following:
>>> flip[0] [[150, 75, 0], [150, 75, 0], [150, 75, 0], [150, 75, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0]]
And of course you can also use save_pixels
to create an image from
the results and view that image – enlarging it as needed in your
image viewer to see it better. We have not provided the expected
image files, but in most cases you should be able to figure out if
the image looks correct based on what the function you are testing
is supposed to do.
The Autograder says that I’m failing a test for this problem, but my actual results match the expected ones. What is going on?
Make sure that you’re using the output_formatted
helper
function that we’ve provided to output the results with
the correct formatting.
I’m failing the Autograder test that is testing a run of the program with inputs of Houston, TX followed by Tampa, FL, but the results look more or less correct. What could be going wrong?
Here are some things to check:
You should only ask for the name of the file once at the beginning of the program, as shown in the sample run.
If the user enters “quit” for the name of the city, you should not ask for the state abbreviation. Instead, the program should end immediately, as shown at the end of the sample run.
You should not include any extraneous print
statements –
e.g., a statement that prints the city and state after the
user enters them.
As usual, you should not include any code in the global scope
(outside of the bodies of your functions). For example, you
should not include a line of code at the end of your file
that makes the call to main
. Rather, you should test your
program by calling main()
from the console.
In general, you should try to ensure that the output of your program matches the provided sample run as closely as possible.
Last updated on April 28, 2025.