CS21 Lab 1: First Programs

Due Saturday, September 10, before midnight

This lab assignment requires you to write a few python programs. First, run update21. This will create the cs21/labs/01 directory 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. Exercise goals
Oberon

Dogs love treats, but they need to work for them to stay healthy. Write a program treats.py in the cs21/labs/01 directory that asks a user how many miles their dog walked and outputs the equivalent number of treats they could eat to offset the calories burnt. Assume walking a mile burns 25 calories, while a biscuit contains 20 calories, a spoon of peanut butter contains 94 calories, and a scoop of ice cream contains 150 calories. Here are some sample runs of such a program:

$ python treats.py
This program will tell you how many treats you earned during your walk!

Enter number of miles walked: 10

You burned 250.0 calories which is equivalent to:
12.5 biscuits OR
2.65957446809 spoons of peanut butter OR
1.66666666667 scoops of ice cream

$ python treats.py
This program will tell you how many treats you earned during your walk!

Enter number of miles walked: 1.8

You burned 45.0 calories which is equivalent to:
2.25 biscuits OR
0.478723404255 spoons of peanut butter OR
0.3 scoops of ice cream


2. Splitting the bill

Write a program to help a party of friends divide up a restaurant bill equally. The program should ask for the total bill, the tip to add, and how many people are splitting the bill. Here is how your program should look:

$ python splitbill.py

Enter the amount of the bill $55.34
Enter the percent tip you'd like to give: 20
Enter the number of people sharing the bill: 3

The tip is ............$11.068
The total cost is .....$66.408
The cost per person is $22.136

$ python splitbill.py

Enter the amount of the bill $162.29
Enter the percent tip you'd like to give: 15
Enter the number of people sharing the bill: 8

The tip is ............$24.3435
The total cost is .....$186.6335
The cost per person is $23.3291875

Extra Challenge

This does not affect your grade so please only attempt this after completing the rest of your lab

The output above is not realistic since money does not come in smaller denominations than 1 cent (i.e., we do not need more than two digits after the decimal point). Use string formatting to clean up the output like this:

$ python splitbill.py

Enter the amount of the bill $162.29
Enter the percent tip you'd like to give: 15
Enter the number of people sharing the bill: 8

The tip is ............$  24.34
The total cost is .....$ 186.63
The cost per person is $  23.33

3. Swimming

While watching the Olympics, you are inspired to calculate how fast you could swim an event. However, you only know how fast you can swim one 50 meter lap. Write a program called swim.py that asks the user for the time it takes to swim one lap and then how many total laps to calculate. Then, print out how many seconds it would take to swim each lap, adding 10% for each lap to model fatigue. Your output should be formatted similar to the example below.

Here is a sample run, showing how your program should work:

$ python swim.py
This program calculates your projected swim time

Time to swim one lap (in seconds): 30      
Number of laps: 10

Laps	Split	TotalTime
----	-----	---------
1	30.0	30.0
2	33.0	63.0
3	36.3	99.3
4	39.93	139.23
5	43.923	183.153
6	48.3153	231.4683
7	53.14683	284.61513
8	58.461513	343.076643
9	64.3076643	407.3843073
10	70.73843073	478.12273803

TIPS

Extra Challenge

As mentioned, tabs are not perfect. However, we can use print formatting (see challenge for Part 2) to fix this. Try it out if you are interested

      Laps      Split Total Time
      ----      ----- ----------
         1      30.00      30.00
         2      33.00      63.00
         3      36.30      99.30
         4      39.93     139.23
         5      43.92     183.15
         6      48.32     231.47
         7      53.15     284.62
         8      58.46     343.08
         9      64.31     407.38
        10      70.74     478.12

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.