CS21 Lab 1: First programs

Due by 11:59pm Tuesday, Sep 11, 2012

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
The program handin21 will only submit files in this labs directory, so make sure your programs are in this directory!

1. Football (called soccer in this country)

In football tournaments, during the group stage, teams are ranked with a points system, where a win is worth 3 points, a draw 1 point, and a loss 0 points. Write a program called football.py that asks the user for the number of team wins, losses, and draws, and then calculates the total points for that team.

Here are some sample runs of such a program:

$ python football.py

  Number of wins: 2
Number of losses: 1
 Number of draws: 1

Your team has  7  points.

$ python football.py

  Number of wins: 10
Number of losses: 10
 Number of draws: 0

Your team has  30  points.

$ python football.py

  Number of wins: 0
Number of losses: 3
 Number of draws: 1

Your team has  1  points.

2. Dividing by 7

Dividing any integer by 7 always results in a sequence of numbers that contains 142857. Show this by writing a program called sevens.py that asks the user for a final number, and then calculates and shows all divisions from 1/7 to the final number divided by 7. For example, if the user enters 25, show the results of 1/7, 2/7, and so on all the way to 25/7 (remember the difference between integer and floating-point division in python!).

Here are some sample runs of such a program:

 
$ python sevens.py

final number: 4

1 / 7.0 =  0.142857142857
2 / 7.0 =  0.285714285714
3 / 7.0 =  0.428571428571
4 / 7.0 =  0.571428571429

$ python sevens.py

final number: 13

1 / 7.0 =  0.142857142857
2 / 7.0 =  0.285714285714
3 / 7.0 =  0.428571428571
4 / 7.0 =  0.571428571429
5 / 7.0 =  0.714285714286
6 / 7.0 =  0.857142857143
7 / 7.0 =  1.0
8 / 7.0 =  1.14285714286
9 / 7.0 =  1.28571428571
10 / 7.0 =  1.42857142857
11 / 7.0 =  1.57142857143
12 / 7.0 =  1.71428571429
13 / 7.0 =  1.85714285714

3. Converting calories

Here's a conversion table for calories:

Write a program called calories.py that asks the user for a number of calories and then calculates how many grams of fat, protein, carbohydrates, and alcohol that would be.

Here are some sample runs of such a program:

$ python calories.py

How many calories?  --> 9

here's how that converts: 
------------------------
          grams fat:  1.0
      grams protein:  2.25
grams carbohydrates:  2.25
      grams alcohol:  1.28571428571

$ python calories.py

How many calories?  --> 400

here's how that converts: 
------------------------
          grams fat:  44.4444444444
      grams protein:  100.0
grams carbohydrates:  100.0
      grams alcohol:  57.1428571429


Submit

Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt.

You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize, after handing in some programs, that you'd like to make a few more changes to them.

Remember: for this lab, handin21 will only submit files from your cs21/labs/01 directory. If you create your programs in a different directory, use the unix mv or cp commands to move or copy them into the cs21/labs/01 directory. For example:

 cp  myprog.py  ~/cs21/labs/01/myprog.py