CS21 Lab1: First Programs

Due Saturday 12 September 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. Textbook Savings

This year we are using a free online textbook for the course. The money saved from buying an outrageously expensive textbook could be used to purchase something more interesting. Write a program that asks the user for the amount of money saved from not having to buy a textbook. Next, prompt the user to type something else he/she would like to purchase, as well as the purchase price of the item. Your program should then print how many of the desired item one could purchase with the textbook savings. Here are some sample runs showing how your program should work:

$ python booksavings.py

Enter textbook savings: 75.25
What would you like to purchase instead of a book: coffee
Enter item cost: 1.50
You could buy 50 coffee(s) with the textbook savings
$ python booksavings.py

Enter textbook savings: 125
What would you like to purchase instead of a book: goat
Enter item cost: 55
You could buy 2 goats(s) with the textbook savings
$ python booksavings.py

Enter textbook savings: 80
What would you like to purchase instead of a book: Toyota Tacoma
Enter item cost: 32000
You could buy 0 Toyota Tacoma(s) with the textbook savings

2. Trip Cost

Write a program to compute the cost of a multi-day trip for one or more people. Your program should first prompt the user for the travel cost per person. Assume that the travel cost is a plane/bus/train ticket cost that each person must pay. Next, prompt the user for the food and lodging cost per day. Next, ask the user for the number of days of the trip and the number of people traveling. Your program should compute and print the total cost per person for the trip including travel, food, and lodging. Finally print the total cost for the group. Assume that food and lodging are paid for each day of the trip, but the travel cost is only paid once, regardless of trip length. Some sample runs of the program are shown below.


$ python tripcost.py

Let's go on a trip

What is the travel cost per person?: 400
What is the food/lodging cost per person per day?: 135
How many days will you be traveling? 5
How many people are going? 2

Cost per person $1075.00
Total cost $2150.00


$ python tripcost.py

Let's go on a trip

What is the travel cost per person?: 40
What is the food/lodging cost per person per day?: 30
How many days will you be traveling? 2
How many people are going? 4

Cost per person $100.00
Total cost $400.00


3. Driving Time
How long would a road trip take at various travel speeds, e.g., light traffic, or rush hour? Write a program to find out. Prompt the user for a minimum travel speed in miles per hour and a maximum travel speed. Next, prompt the user for the trip distance. Your program should print a table of travel times for each speed between the minimum and maximum speeds, including both endpoints, at one mile per hour increments.

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

$ python drivetime.py
Enter slowest speed in mph: 40
Enter fastest speed in mph: 60
Enter driving distance in miles: 100

Time chart for 100 miles

speed  time
---------------
 40    2.50 hrs
 41    2.44 hrs
 42    2.38 hrs
 43    2.33 hrs
 44    2.27 hrs
 45    2.22 hrs
 46    2.17 hrs
 47    2.13 hrs
 48    2.08 hrs
 49    2.04 hrs
 50    2.00 hrs
 51    1.96 hrs
 52    1.92 hrs
 53    1.89 hrs
 54    1.85 hrs
 55    1.82 hrs
 56    1.79 hrs
 57    1.75 hrs
 58    1.72 hrs
 59    1.69 hrs
 60    1.67 hrs

$ python drivetime.py
Enter slowest speed in mph: 25
Enter fastest speed in mph: 30
Enter driving distance in miles: 45

Time chart for 45 miles

speed  time
---------------
 25    1.80 hrs
 26    1.73 hrs
 27    1.67 hrs
 28    1.61 hrs
 29    1.55 hrs
 30    1.50 hrs
Optional Challenge
This portion of the program is NOT required and will not be graded. You should only attempt this optional feature if you have completed the required portions of the lab. Extend your drivetime program to print the time in hours and minutes instead of fractional hours. See the example below
$ python drivetime_extra.py
Enter slowest speed in mph: 25
Enter fastest speed in mph: 30
Enter driving distance in miles: 45

Time chart for 45 miles

speed  time
---------------
 25    1:48 hrs
 26    1:43 hrs
 27    1:40 hrs
 28    1:36 hrs
 29    1:33 hrs
 30    1:30 hrs


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.