CPTR 124 Fundamentals of Programming
This lab provides experience with variables, input, output, and simple arithmetic in Python.
Forming a Team
You are encouraged to choose a partner from this class as a teammate. You two will work together on this assignment.You are not required to work with someone else; you may work alone if you prefer.
Familiarize yourself with the assignment
Read over the complete lab before you begin creating your project and writing Python code. Be sure you can work out the solutions with pencil and paper before you start coding. If you cannot do the arithmetic by hand, you have little hope of writing a computer program to do so.
A good approach is to work out by yourself the mathematical stategy to solve each problem. Next, see how your partner solved each of the problems. Agree on the best strategy for each problem and then work together on implementing that strategy in a Python program. If you both feel confident in your Python programming skills, you could work separately coding each program and then get together to build a final version incorporating the best ideas from both.
Ground rules
You may use any of the techniques described in the textbook in Chapters 1–3. Specifically, you may not use conditional statements in any of these programs. The purpose of this lab is to better understand Python's mathematical operators. All the requirements of this lab can be met by the clever application of these mathematical operators.
What to do
Write a Python program that consists of two parts:
Computing the amount of money given
a quantity of bills and coins
The program should request eight integer values from the user (see the sample program run below). These eight integer values represent, respectively, the number of twentydollar bills, tendollar bills, fivedollar bills, onedollar bills, quarters, dimes, nickels, and pennies. The user should be able to enter any nonnegative integer value for these entries. You program does not need to worry about bad input (negative values, floatingpoint numbers, or nonnumbers). Once provided with these values, the program should display the amount of money in the normal dollars and cents way; for example, $12.75.
You will need to use addition, multiplication, division, and modulus in your calculations, but the arithmetic is not complicated. Avoid floatingpoint arithmetic in your calculations; use
//
rather than/
, and do not use numbers with decimal points like 5.00. Since floatingpoint numbers inherently are inexact, in some situations your results can be off by a penny if you use floatingpoint arithmetic.Your program first should compute the total number of cents entered by the user (a dime is 10 cents, a twentydollar bill is 2000 cents, etc.). Once you have the total number of cents, you need to display the amount in the familiar $xxx.xx dollars and cents format. You can divide the number of cents by 100 using floatingpoint division to get the quantity in dollars (for example, 1225 cents is 12.25 dollars). Since this value is a floatingpoint number, and the computer cannot represent many floatingpoint values exactly, we need to ensure that only the first two digits behind display. Assuming your variable representing the total number of cents is named
cents
, the following statement will work nicely:print("${:.2f}".format(cents/100))The somewhat cryptic
{:.2f}
notation is the trick that rounds the displayed floatingpoint value to two digits behind the decimal point. 
Monetary amount to currency
Next, do the reverse of the above problem. Your program should request an amount of money from the user, such as 2.27. (Note that this is a floatingpoint number with a decimal point.) The program then displays the number of twentydollar bills, tendollar bills, fivedollar bills, onedollar bills, quarters, dimes, nickels, and pennies that would constitute the amount of money entered by the user. The numbers of each denomination must be as small as possible; for example, if the user entered 2 dollars and 5 cents, the program would report 0 twenties, 0 tens, 0 fives, 2 ones, 0 quarters, 0 dimes, 1 nickel, and 0 pennies, rather than 205 pennies or 45 nickels.
As in Part 1, limit yourself to integer arithmetic. This means the first thing you should do is convert the number entered by the user into cents (for example, 2.27 is 227 cents). You can do this by multiplying the input value by 100 and using the
int
function to convert the answer to an integer. Compute the total number of cents and then partition the cents into 2000s of cents (for twenties), 1000s of cents (for tens), etc.How do we convert the user's input, a string representing a floatingpoint number, into cents? The following statement grabs the user's input as a string of characters:
cash = input('Please enter total cash (no dollar sign):')At this point the
cash
variable refers to a string. The next statement uses thefloat
function to convert the user supplied text to a floatingpoint number:money = float(cash)So far, so good. The
money
variable refers to the floatingpoint number the user entered. The next statement multiplies by the user's input by 100 to get the number of cents and converts it to an integer:cents = int(100*money)The
cents
variable now holds the integer number of cents the user entered. This works, but it is not perfect. You can use a statement like this one initially as you are writing your program, but can you find its flaw? How would you modify this last statement to make it work correctly all the time? 
Program Behavior
The following shows a sample run of the program.
Please enter the quantity for each of the indicated denominations.
Twenty dollar bills: 2
Ten dollar bills: 1
Five dollar bills: 0
One dollar bills: 2
Quarters: 1
Dimes: 3
Nickels: 0
Pennies: 4
Cash entered: $52.59
=====================================
Please enter total cash (no dollar sign): 34.17
Twenties: 1
Tens: 1
Fives: 0
Ones: 4
Quarters: 0
Dimes: 1
Nickels: 1
Pennies: 2
The program's output is shown in white, and the yellow numbers represent the values the user entered. Your program should behave as closely as possible to the way this program appears in terms of text printed and formatting. Do not worry about bad user input; your program need only work correctly when provided with valid numbers.

Computing the amount of money given
a quantity of bills and coins

Check out
I will review your lab with you before you leave. Be prepared to answer questions about any parts of your program. After you have been checked out, please submit your Python source code to http://eclass.e.southern.edu.

Log out
Don't forget to log out on your lab workstation and take your USB drive before you leave the lab.