CPTR 124 Fundamentals of Programming


This lab provides experience with variables, input, output, and simple arithmetic in Python.
  1. 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.

  2. 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.

  3. 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.

  4. What to do

    Write a Python program that consists of two parts:

    1. 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 twenty-dollar bills, ten-dollar bills, five-dollar bills, one-dollar bills, quarters, dimes, nickels, and pennies. The user should be able to enter any non-negative integer value for these entries. You program does not need to worry about bad input (negative values, floating-point numbers, or non-numbers). 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 floating-point arithmetic in your calculations; use // rather than /, and do not use numbers with decimal points like 5.00. Since floating-point numbers inherently are inexact, in some situations your results can be off by a penny if you use floating-point arithmetic.

      Your program first should compute the total number of cents entered by the user (a dime is 10 cents, a twenty-dollar bill is 2000 cents, etc.). Once you have the total number of cents, you want to display the dollar sign, then the amount as 100s of cents (dollars), then the decimal point, then the remaining tens of cents (the digit immediately following the decimal point), and finally the remaining cents left over (the last digit printed).

      Be careful to ensure your program does not, for example, print five cents as $0.5 instead of $0.05 (note the zero in the tens place). Similarly, it should print $0.50 for fifty cents and not $0.5 (missing zero, but from a different place).

    2. 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 floating-point number with a decimal point.) The program then displays the number of twenty-dollar bills, ten-dollar bills, five-dollar bills, one-dollar 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 floating-point 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 the eval function to evaluate the string as a floating-point number:

      money = eval(cash)

      So far, so good. The money variable refers to the floating-point 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?

    3. 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.

  5. 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.

  6. Log out

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