Exercises for Lesson 13

Back to Lesson 13

Exercise 1: Nested for loops

Part a: tracing a nested loop

Consider the following program. How many times does the print() statement execute? What is printed when this program runs?

def nested(a, b):
    for i in range(a):
        for j in range(b):
            print(i, j, i*j)

def main():
    x = 2
    y = 5
    nested(x, y)

if __name__ == "__main__":
    main()

Part b: writing a nested loop

Write a program to print out all possible combinations when rolling two six-sided dice.

Here is the beginning of what it should print:

1+1 (2)
1+2 (3)
1+3 (4)
1+4 (5)
1+5 (6)
1+6 (7)
2+1 (3)
2+2 (4)
...

Back to Lesson 13

Exercise 2: Creating an image

Write a function that generates (and returns) a 400x200 image with one randomly-chosen color per row. To do this, fill in createRandomRowImage in the program below.

from graphics import *
import random

def chooseColor():
    """
    Randomly chooses a color

    return: the color (using color_rgb)
    """
    r = random.randint(0,255) # unlike randrange, randint is inclusive on both ends
    g = random.randint(0,255)
    b = random.randint(0,255)
    return color_rgb(r, g, b)

def createRandomRowImage():
    """
    Creates a 400x200 image and assigns each row a random color.

    returns: the random-row image
    """
    # Generate a blank 400x200 image (not starting from a file)
    image = Image(Point(0,0), 400, 200)

    return None # replace with your code

def main():
    # Create the random-row image
    image = createRandomRowImage()
    width = image.getWidth()
    height = image.getHeight()

    # Create the window
    win = GraphWin("Image Processing", width, height)

    # Draw the image
    image.move(width/2, height/2)
    image.draw(win)

if __name__ == "__main__":
    main()

Back to Lesson 13

Exercise 3: Getting image colors

Complete the following program to report the pixel color where a user clicked on an image, until the user has clicked ten times.

from graphics import *

def reportClick(image, p):
    """
    Prints out the location and color where the user clicked on the image.

    image: an Image
    p: the Point where the user clicked
    """
    pass # TODO: replace with your code

def main():
    # Load the image
    image = Image(Point(0,0), "cheddar.gif")
    width = image.getWidth()
    height = image.getHeight()

    # Create the window
    win = GraphWin("Image Processing", width, height)

    # Draw the image
    image.move(width/2, height/2)
    image.draw(win)

    # Report the color and location of ten user clicks
    for i in range(10):
        p = win.getMouse()
        reportClick(image, p)

    # Exit after one more click
    win.getMouse()
    win.close()

if __name__ == "__main__":
    main()

When you run this and click on an image, you should get output like the following:

Click (x=373, y=260): r=102, g=85, b=51.

Back to Lesson 13

Exercise 4: Grayscale based on human perception

We can choose the grayscale value for a pixel using the following calculation:

Y = 0.2126 * R + 0.7152 * G + 0.0722 * B

Fill in the following program. It should compute Y for each pixel, and color that pixel (Y,Y,Y) instead.

from graphics import *

def getGrayscaleColor(r, g, b):
    """
    Converts the pixel to grayscale using the formula
       Y = 0.2126*r + 0.7152*g + 0.0722*b.

    r: the red channel (int in the range 0 to 255)
    g: the green channel (int in the range 0 to 255)
    b: the blue channel (int in the range 0 to 255)
    returns: the result of color_rgb (int in the range 0 to 255)
    """
    return None # replace with your code

def createGrayscaleImage(origImage):
    """
    Creates a copy of the original image, and colors it grayscale.

    origImage: the original image (graphics.py Image object)
    returns: the grayscale image
    """
    return None # replace with your code

def main():
    # Load the original image
    image = Image(Point(0,0), "cheddar.gif")
    width = image.getWidth()
    height = image.getHeight()

    # Create the window to display both images
    win = GraphWin("Image Processing", width*2, height)

    # Draw the original image
    image.move(width/2, height/2)
    image.draw(win)

    # Create the grayscale image
    grayscaleImage = createGrayscaleImage(image)
    grayscaleImage.move(width, 0)
    grayscaleImage.draw(win)

    # Exit after the user clicks
    win.getMouse()
    win.close()

if __name__ == "__main__":
    main()

Back to Lesson 13

Exercise 5: Mixing for and while

Consider the following program. How many times does the print() statement execute? What is printed when this program runs?

def divide(n):
    for i in range(n):
        j = i
        while j > 0:
            print(i, j)
            j = j // 2

def main():
    num = 6
    divide(num)

if __name__ == "__main__":
    main()

Back to Lesson 13