COSC 101 Homework 2: Fall 2014

The due date for this homework is Wednesday, September 17, 11:55pm. Upload your solution to moodle.

Introduction

This assignment is designed to give you a first introduction to writing programs in python! By completing this assignment you will demonstrate that you understand a number of important concepts:

We encourage you to start early.

Your assignment

Your task is to complete following steps:

  1. Complete program 1 (Mad Libs) in the file called hw2_madlibs.py that is included with this homework.
  2. Complete program 2 ("Are we there yet?") in the file called hw2_road_trip.py that is included with this homework. This is probably the hardest of the three programs.
  3. Complete program 3 (Turtle Socks) in the file called hw2_turtles.py that is included with this homework.
  4. Review the grading criteria at the end of this assignment.
  5. (OPTIONAL) Try the challenge problem!
  6. Upload each of the three files to moodle. Please upload them as three separate files (i.e., do not zip them).

Program 1: Mad libs

Write a short program that asks for a noun (string) and two verbs (both strings). (You can assume that both verbs are in third person form.) Using the three inputs, your program should print the sentence:

If it <verb 1> like a <noun> and <verb 2> like a <noun>, it probably is a <noun>.

Below is an example execution of the program where the user types "duck," "walks", and "talks.":

What's the noun? duck
What's the first verb? walks
What's the second verb? talks

If it walks like a duck and talks like a duck, it probably is a duck.

A second example where the user types "loon," "swims," and "dives.":

What's the noun? loon
What's the first verb? swims
What's the second verb? dives

If it swims like a loon and dives like a loon, it probably is a loon.

Program 2: Are we there yet?

Road trip! Write a program that asks for a driver's name, the distance (in miles) and the speed (in miles per hour) and reports how long it will take the driver to arrive at the destination. The time should be reported in units of days, hours, minutes, and seconds, rounded to the nearest second. The printed numbers should be of type int, however, you may need to work with floats during the computation.

Your program's output should look exactly like the examples provided below.

Here is an example execution where the user types "Jack" for the driver's name, "3000" for the distance and "50" for the speed:

Who is driving? Jack K.
How far away is the destination (in miles)? 3000
How fast is Jack K. driving (in mph)? 50
It will take Jack K. 2 days, 12 hours, 0 minutes, and 0 seconds

Another example. This time the user types "Ryan" for the driver's name, "120" for the distance and "30" for the speed:

Who is driving? Ryan
How far away is the destination (in miles)? 120
How fast is Ryan driving (in mph)? 30
It will take Ryan 0 days, 4 hours, 0 minutes, and 0 seconds

Here is another example. The driver is only going 3 miles but he's going 120 mph, which is equivalent to 2 miles per minute! Going 3 miles takes one and a half minutes, but notice how the program breaks it down into minutes and seconds:

Who is driving? Dwayne
How far away is the destination (in miles)? 3
How fast is Dwayne driving (in mph)? 120
It will take Dwayne 0 days, 0 hours, 1 minutes, and 30 seconds

One more example, where the driver is going a mile per minute. Notice how \(\frac{3}{4}\) of a mile translates to 45 seconds, which is \(\frac{3}{4}\) of a minute:

Who is driving? Vin D.
How far away is the destination (in miles)? 45.75
How fast is Vin D. driving (in mph)? 60
It will take Vin D. 0 days, 0 hours, 45 minutes, and 45 seconds

Some important details and a few hints:

Program 3: Turtle Socks

Use the turtle module to draw this rather colorful sock. Your program should ask the user for the length of the sock and then draw a sock of that length and with the colors and shapes matching the drawing below. The picture below shows you the dimensions of the different shapes that make up the sock in terms of the overall sock length. For example, if the sock is of length \(L\) then the rectangle has side lengths of \(\frac{2}{3}L\) and \(\frac{1}{3}L\).

Sock drawing side lengths

Sock drawing side lengths

Important detail. For this assignment, you are not allowed to use the goto method on turtle objects. Instead, move the turtle using only forward and backward and left and right. In the zip file that comes with this assignment, you will find a reference sheet on turtle commands.

You should write your solution in the file named hw2_turtles.py that is included with this homework. When you open that file in IDLE, you will see that it already contains a lot of code! This is some code to get you started. Instead of drawing a sock, it draws two orange squares. Nevertheless, it should serve as a useful guide on how to create a turtle, move it around, color in polygons, move the turtle without drawing, etc. Modify this code as needed to draw a sock instead of these boring orange squares.

Finally, a quick geometry review of the isosceles right triangle that appears in the figure. You'll notice that if the sides are length s, the hypotenuse has length \(s\sqrt{2}\). You can use the math module to compute \(\sqrt{2}\).

Geometry of isoceles right triangle

Geometry of isoceles right triangle

Here are two screenshots of the turtle window in IDLE. In the first one, my program asked the user for a sock length and they entered 100, a sock for a baby.

Example: user input is 100

Example: user input is 100

This second screenshot is what it looked like when the user entered 300, a sock for big foot.

Example: user input is 300

Example: user input is 300

Grading

Your assignment will be graded on two criteria:

  1. Correctness: this document contains several examples for each program. Be sure that you run your program once for each example and make sure it works correctly for each one! [80% = 10+50+20]
  2. Program design and style [20%]: style and program design become increasingly important the more complex your program becomes. For these first programs, adhere to the following guidelines:

Challenge problem

The challenge problem this week is to do something creative. Make your own art work using the turtle module. What you do is completely up to you, but to receive full credit for the challenge problem, you must draw something that is more complex than the turtle sock. For example, can you use a for loop to draw an interesting shape or pattern?