CS21 Lab1: First Programs

Due Saturday (September 13) 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. Halloween candy

Write a program that figures out how many pieces of candy you can offer to each trick-or-treater that visits your abode. Assume that each bag of candy contains 25 pieces. Here are some sample runs showing how your program should work:

$ python candy.py

How many bags of candy do you have? 2
How many trick-or-treaters do you expect? 10

You can give each trick-or-treater 5 pieces of candy.
Happy Halloween!

$ python candy.py

How many bags of candy do you have? 3
How many trick-or-treaters do you expect? 20

You can give each trick-or-treater 3 pieces of candy.
Happy Halloween!

2. Sharing the bill

Write a program that will help people figure out how to divide up a restaurant bill equally. Here's how the program should work:

$ python sharing.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.07
The total cost is .....$  66.41
The cost per person is $  22.14

$ python sharing.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. Walking steps
Pedometers, like the fit bit, allow people to monitor their step count and show them how far they've walked in a day. On average a person's step length is approximately 41.4% of their height. Your program should ask for a person's height, approximate their step length, and then create a table showing them how their steps taken equate to miles traveled. Recall that there are 36 inches per yard and 1760 yards per maile.

Below are some sample runs showing how your program should work. Notice that for a person who is 5 feet tall (or 60 inches), ten thousand steps does not take them as far as a person who is 6 feet tall (or 72 inches).

$ python walk.py

What is your height in inches? 60
Your estimated step length is 24.84 inches long.

 STEPS  MILES
  1000   0.39
  2000   0.78
  3000   1.18
  4000   1.57
  5000   1.96
  6000   2.35
  7000   2.74
  8000   3.14
  9000   3.53
 10000   3.92

$ python walk.py

What is your height in inches? 72
Your estimated step length is 29.808 inches long.

 STEPS  MILES
  1000   0.47
  2000   0.94
  3000   1.41
  4000   1.88
  5000   2.35
  6000   2.82
  7000   3.29
  8000   3.76
  9000   4.23
 10000   4.70

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.