CPSC 115L: Introduction to Computing Spring 2010

Laboratory 4 (Graded): Printing invoices

February 16, 17

This laboratory will be graded in numerical points. Unlike the pass/fail laboratories, you are expected to work alone; furthermore, the instructor and TA will provide help only if there are technical problems with your workstation. You must hand in your solution by the end of the laboratory period.

Objectives

The main objectives of this laboratory are
  1. to evaluate your ability to design and implement your own program by following the three basic steps of software development, and
  2. to evaluate your ability to design, implement and use functions in your program.

1. Printing an invoice

Design and implement a Python script to print an invoice for Alyssa's coffee shop called Alyssa's Coffee. This coffee shop sells only one type of coffee beans, House Blend, and its price is based on the current market value. Each order ships for 75¢ per pound, plus a fixed overhead charge of $1.50. Your script should calculate the total cost of an order based on (i) the current price for a pound and (ii) how many pounds are ordered. Your script should behave as follows:
Welcome to Alyssa's Coffee!
Enter today's price for one pound: 9.95
How many pounds for this order? 6
With 9.95 dollars a pound, the total for 6 pounds is 65.7 dollars.
Note. To print text with apostrophes, use double quotations; for example,
print "Welcome to Alyssa's Coffee!"
As before, first write down on a piece of paper your I/O specification and algorithm in pseudocode. Then implement your algorithm in a Python script named coffee1.py. Run your script with five test cases, and save the snapshots of your test runs in a text file named coffee1.out. As done in laboratories, put a file header at the top of each file.

2. Handling multiple orders

Your script from Part 1 only handles a single order. For Part 2, modify your implementation so that it can handle multiple orders. For this, design and implement a function named print_invoice that handles a single order. The function print_invoice should have two parameters, one for the current price for a pound and another for the amount ordered in pounds, and outputs the total cost. After implementing the function print_invoice, write a code segment to invoke it three times. Your script should behave as follows:
Welcome to Alyssa's Coffee!
Enter today's price for one pound: 9.95
How many pounds for the first order? 6
With 9.95 dollars a pound, the total for 6 pounds is 65.7 dollars.
How many pounds for the second order? 2
With 9.95 dollars a pound, the total for 2 pounds is 22.9 dollars.
How many pounds for the third order? 3
With 9.95 dollars a pound, the total for 3 pounds is 33.6 dollars.
Implement your function and write a code segment to invoke it in a Python script named coffee2.py. Run your script with five test cases, and save the snapshots of your test runs in a text file named coffee2.out. Put a file header at the top of each file. When completed, show your work to the instructor or TA.

3. Printing a Price List

Alyssa's Coffee wants to have a price list showing the price of multiple pounds of coffee. For example, here's the price list for up to 10 pounds of coffee at 9.95 per pound:

Price List for Bulk Purchases at  9.95  dollars per pound
Pounds Cost    With 5% Discount
1      12.2    11.7
2      22.9    21.9
3      33.6    32.11
4      44.3    42.31
5      55.0    52.51
6      65.7    62.72
7      76.4    72.92
8      87.1    83.12
9      97.8    93.32
10     108.5   103.53

Write a function named print_price_list() that takes two parameters, the price per pound, and n, for number of pounds, and prints a 3-column price list that includes number of pounds in column 1, total cost in column 2, and cost with a 5% discount per pound in column three. The function should print a price list with n rows. Don't forget the overhead, which applies to the whole order.

Note, to get columns aligned, you can print a tab character ('\t'). Also, to round your price to two decimal places use the Python round(x, n), which rounds x to n decimal places.

What to hand in

Upon completion of your laboratory, submit the following in paper:

* CPSC 115L home page
Valid HTML 4.01!