Introduction to Computer Science
Union College
Spring 2009

Homework 3 - due Wednesday, April 22, before class

1) Old MacDonald had a farm

Write a program that prints the lyrics of the song "Old MacDonald". Your program should print the lyrics for five different animals, similar to the example verse below:

Old MacDonald had a farm,  Ee i ee i oh!
And on that farm he had some cows, Ee i ee i oh!
With a moo-moo here, And a moo-moo there
Here a moo, there a moo, Everywhere a moo-ooo
Old MacDonald had a farm, Ee i ee i oh!
Your program should use functions.

2) Drawing faces

Write a function (for a pygame program) that draws a face. The function should meet this specification:

drawFace(x, y, color)
where x is the x-coordinate of the center, y is the y-coordinate of the center, and color is the color.

Demonstrate your function by writing a pygame program that draws several faces in different locations.

To get started and to remind yourself how to draw shapes with pygame, you can use this program. (That's what you used in class the very first week.)

3) Random numbers

Python has a built-in library/module called random. This module provides a function called randint that generates a random integer (from a given range of integers).

The function is documented in the documentation of the random module at Use this page to find out how to use the function random.randint, and write a (very) small program that just print a random number between 0 and 100.

Partly, this is an exercise in using functions. But the other purpose of this exercise is to get you used to finding your way through Python documentation. There is probably a lot of information on the page that you don't really understand - one important programming skill is to navigate through cryptic documentation and find the bits you need.

4) Math quiz

From Gaddis, page 221

Write a program that gives a simple math quiz. The program should display two random numbers that are to be added, such as:

247 + 129 = 
The program should then allow the user to enter the answer. If the answer is correct, a message of congratulation should be displayed. If the answer is incorrect, a message showing the correct answer should be displayed.

5) Pizza prices

Write a program that calculates the cost per square inch of a circular pizza given its diameter and overall price.

Ask the user to input the pizza's price and diameter and use three functions: One to compute the area of the pizza, one to compute the cost per square inch, and a main function that ties everything together.

The formula to calculate the area of a circle is A = π * radius * radius. The Python module called math ( defines a constant math.pi that refers to the value of π.

6) Submit

Upload all of your python programs and answers to the questions to Blackboard.

by Kristina Striegnitz