Exercises for Lesson 15

Back to Lesson 15

Exercise 1: Colors

Part a: Guessing colors

For each (r,g,b) triple below, what color do you expect to see?

(i)   (255, 0, 0)

(ii)  (0, 255, 255)

(iii) (120, 0, 120)

(iv)  (255, 128, 0)

Part b: Creating colors

For each color name below, give an (r,g,b) triple that would be close to that color.

(i)   yellow

(ii)  medium gray

(iii) salmon

(iv)  forest green

Back to Lesson 15

Exercise 2: Getting image colors

Complete the following program to report the pixel color where a user clicked on an image (example 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_sleep.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 15

Exercise 3: 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 = 4
    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 15

Exercise 4: 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)
    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)

    # TODO: Color in each pixel
    # (replace the following two lines with your code)
    color = chooseColor()
    image.setPixel(10, 20, color)

    return image

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 15