Lab 8 Compsci 101, Fall 2025
Lab Reflect/Questions and Starter Code
Part 1: Dictionary to Dictionary (15 minutes)
Part 3: Reflection on Debugging (5 minutes)
Part 4: Review A Little on Debugging (5 minutes)
Remember the steps for debugging
Order of Operations and Ways to Debug
Part 5: Debugging Exercises (20 minutes)
As you're completing this lab, complete this form.
You can find the .zip file containing all code linked here.
Consider the following dictionary named data that maps a string that is a netid to a list of strings where each string is the course a student with that netid is currently taking.
Now consider we want to find out for each course, the netid’s of who is in that course. The dictionary above is not setup well for that. Instead, we should take that dictionary data as input and create a dictionary that maps a course to a list of netid’s of students that are in that course. If we use the dictionary above as input, we would get this dictionary:
In the file ReverseDictionary.py, which you downloaded as part of this lab, complete the function reverse that takes as input a dictionary in the format data is in above (netid to list of courses) and returns a dictionary in the format of revdata below (course to list of netid’s in the course).
It is very important to use meaningful variable names since you have two dictionaries with different keys and different values.
Answer the following questions:
In this lab we will explore Images. Each image is made up of pixels. A pixel is a tuple of three RGB values.
Images are a powerful way of conveying information and computers give us a powerful way of modifying images. This lab involves manipulating pixmaps by transforming each color in the original pixmap using the same algorithm (such as darkening, inverting, posterizing, converting it to grey scale). Your programs will then perform several operations on these images to achieve a larger effect.
Images are stored by computers in a variety of formats, such as GIF, JPG/JPEG, TIFF, and PNG. These formats differ in how faithfully they represent the original picture, how well they can be compressed to reduce the space each image takes up, or how well they can be copied from one type of computer to another. However, no matter what format the image is stored in, it can always be represented as a mapping of (x, y) pixel position to a color (the range of colors may be restricted to values of grey or just black and white). Thus, for the remainder of this project, we will refer to all formats of digital images as images.
This week's lab uses the Python Imaging Library (PIL) with info here which provides a simple interface for processing images. We will create basic color filters (RGB color model) that can be applied to images then combine them to create larger effects.
If you haven't yet, you will need to install PIL. In Pycharm, go to the terminal window.
At the prompt, type: pip install Pillow
If that doesn't work, you can also try to type: Python3 -m pip install Pillow;
In lecture we saw a program grayscale.py to take an image and create a new image that is the grayscale version of the previous image. We have included grayscale.py. Try running that program. An image should pop up and a copy of that image in grayscale should also pop up.
Let's look at the code from grayscale.py. We turned the image gray two different ways. One way was using the function grayByPixel which calls the function getGray:
def getGray(r,g,b):
gray = int(0.21*r + 0.71*g + 0.07*b)
return (gray,gray,gray)
def grayByPixel(img, debug=False):
width = img.width
height = img.height
new_img = img.copy()
if debug:
print("creating %d x %d image" % (width,height))
for x in range(width):
for y in range(height):
(r,g,b) = img.getpixel((x,y))
grays = getGray(r,g,b)
new_img.putpixel((x,y),grays)
return new_img
In this function grayByPixel makes a copy of the image, goes through every Pixel in an (x,y) position, creates a gray color of the pixel by calling the function getGray, and puts the new pixel in the copy of the image.
We also saw another way to convert an image to grayscale that called this function grayByData, which also calls getGray above:
def grayByData(img, debug=False):
pixels = [getGray(r,g,b) for (r,g,b) in img.getdata()]
new_img = Image.new("RGB", img.size)
new_img.putdata(pixels)
if debug:
print("created %d x %d gray image" % (img.width,img.height))
return new_img
In the function grayByData, the function uses a list comprehension and the image function im.getdata() to process all the pixels in a stream of data, to create a list of all the pixels turned gray and then uses the image function putdata to put all the new pixels in the list pixels into the image at once.
You will do three things to images.
Answer the following questions based on this code:
In small groups, think back and discuss the last time you had a bug in your code and answer the following questions:
Compare your experience with a classmate by discussing your responses to the previous questions. If you are completing this lab on your own, just record your responses in the lab form.
Debugging is a mindset! Many say that “debugging comes with practice.” This is a new skill that you are learning; if it seems challenging now, it is important to not be too hard on yourself. As when learning anything new, things may be overwhelming at first because you don’t know what to pay attention to. There are many ways to tackle debugging, and as you gain experience, your sense of which “way” to try first gets better.
You may remember that the 5 W’s are:
In the context of debugging, these questions can be translated to:
Answering these questions can help you brainstorm so you can find and squash the bugs in your code.
When you fail a test or get an error, these steps generally will help you identify the problem:
Notice debug is not the only thing on this list! Writing code that reproduces the bug does two things:
Once you can reproduce the problem, here are a few methods to debug:
With this background, we can practice with a few examples. You’ll need to import the lab code from the link above.
In the zip file, you will find 3 examples: ReadQuizScore, RepeatInOrder, and VenmoTracker. These all link to the problem specifications, which may be useful.
For each of the examples, work with your group to find and fix the bug. Make sure to answer the questions in the lab form for each problem.