CS21 Lab1: First Programs

Due Saturday (January 31) before midnight

This lab assignment requires you to write three programs in Python. First, run update21. This will create the cs21/labs/01 directory (if you haven't already) and copy over any starting-point files for your programs. Next, move into your cs21/labs/01 directory and begin working on the Python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$ update21
$ cd cs21/labs/01
$ pwd
/home/your_user_name/cs21/labs/01
We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory!

Programming tips

As you write your first programs, start using good programming practices now:


1. Valentine's Day Special...Dove dark chocolates

Each Dove dark chocolate has 42 calories. Write a program that asks the user how many Dove dark chocolates they ate, then calculates and displays how many calories that is. You may assume the user will enter valid integers. Here are some sample runs showing how your program should work:

$ python dove.py

How many Dove dark chocolates did you eat? 1
That's 42 calories!

$ python dove.py

How many Dove dark chocolates did you eat? 5
That's 210 calories!

$ python dove.py

How many Dove dark chocolates did you eat? 200
That's 8400 calories!

2. 5K race time

Write a program that asks the user to enter a race pace in minutes and seconds (how long it takes to run one mile), and then calculates the race time for a 5K race. Here's how the program should work:

$ python fivek.py

Give me your mile pace (minutes & seconds per mile)
and I'll calculate your 5K race time.

minutes: 8
seconds: 30

5K race time at that pace: 26:21

For the above example, the user intends to run the race at a pace of 8 minutes, 30 seconds per mile, so a 5K (3.1 miles) will take 26 minutes and 21 seconds to finish.

Hint: for this one, it is probably easiest to follow this algorithm:

Here are some additional examples to test your program:

$ python fivek.py

Give me your mile pace (minutes & seconds per mile)
and I'll calculate your 5K race time.

minutes: 6
seconds: 12

5K race time at that pace: 19:13

$ python fivek.py

Give me your mile pace (minutes & seconds per mile)
and I'll calculate your 5K race time.

minutes: 10
seconds: 0

5K race time at that pace: 31:0


3. Calories Burned vs Heart Rate Calculator

Let's see how much exercise you need to do to burn off all of the calories from those Dove chocolates you ate!

This Calories Burned By Heart Rate Calculator uses the following formulas:

Men:
CaloriesBurned = ((AGE * 0.2017) + (WEIGHT * 0.1988)+ (HR * 0.6309) - 55.0969) * DUR / 4.184

Women:
CaloriesBurned = ((AGE * 0.074) - (WEIGHT * 0.1263) + (HR * 0.4472) - 20.4022) * DUR / 4.184

where the AGE is in years, the WEIGHT is in kg, HR is the heart rate in beats-per-minute, and DUR is the exercise duration in minutes.

Write a program that asks the user for AGE, WEIGHT, exercise DURATION, and a starting HEARTRATE, and then displays a table of values, using the above formulas, for various HEARTRATEs. Your table should start with the given HEARTRATE, but include 10 more, evenly spaced by 10s.

Below are some sample runs showing how your program should work.

$ python cals.py

Calories Burned vs Heart Rate Calculator

                Age in years: 49
                Weight in kg: 72.5
Exercise Duration in minutes: 30
   Minimum Heart Rate in bpm: 70

 HR  CB (Men)  CB (Women)
---- --------------------
 70      95.8      38.5
 80     141.0      70.6
 90     186.3     102.6
100     231.5     134.7
110     276.8     166.8
120     322.0     198.8
130     367.2     230.9
140     412.5     263.0
150     457.7     295.0
160     502.9     327.1
170     548.2     359.2

$ python cals.py

Calories Burned vs Heart Rate Calculator

                Age in years: 20
                Weight in kg: 54.4
Exercise Duration in minutes: 60
   Minimum Heart Rate in bpm: 85

 HR  CB (Men)  CB (Women)
---- --------------------
 85     191.8     175.2
 95     282.3     239.4
105     372.8     303.5
115     463.3     367.6
125     553.7     431.7
135     644.2     495.9
145     734.7     560.0
155     825.2     624.1
165     915.6     688.3
175    1006.1     752.4
185    1096.6     816.5


Submit

Remember you may run handin21 as many times as you like. Each time you run it new versions of your files will be submitted. Running handin21 after you finish a program, after any major changes are made, and at the end of the day (before you log out) is a good habit to get into.