Getting Started: understanding how Floats take over

In this opening section, we’ll play around to get an understanding of important functions on numbers, and also how floating point numbers (numbers with a decimal point) take over expressions when they are present.

Open the Python shell and type the expressions below. Try to predict the output of the interpreter before pressing the ENTER key.

a = 3
x = 1.5
type( a )
type( x )
type( a * x )
type( a * 100 )
type( 1 )
type( 1.0 )
type( "1" )
a / 3
a // 3
type( a/3 )
type( 5/4 )
type( 5//4 )
type( int( 1.5 ) )
int( 1.5 )
round( 1.6 )
round( 1.4 )
round( -1.8 )
type( round( 1.4 ) )
round( -1.8 )
abs( -4 )
abs( 10 )
type( abs( -19.3 ) )
type( abs( -4 ) )

Note how once something is a float, it forces whatever it gets combined with to yield a result that is float. That is, except for the int() function, of course!

Warm Up Exercises

  • Write a small program (either in the Python Shell, or in the Edit window), that asks the user to enter two integers, and then prints out the integer division of the first number by the second number.

  • Here is a sample of what your output should look like Python Shell
  • You don’t need to turn this exercise in! They’re just to get your brain working :-)

Cash Machine

In the remainder of this lab, we will write a program that takes an arbitrary amount of money (dollars and cents) and figures out how to break it down into the smallest number of 20-, 10-, 5-, and 1-dollar bills, plus change (in quarters, dimes, nickels, and pennies).

For those new to US money:

  • a quarter = 25 cents = $0.25
  • a dime = 10 cents = $0.10
  • a nickel = 5 cents = $0.05
  • a penny = 1 cent = $0.01

Setting up

  • Create a new file for this lab
  • Add the usual header
  • Below the header, create 4 different comment lines that will serve as an outline of your program
  # get the initial amount
  
  # compute number of bills
  
  # compute number of coins
  
  # output number of bills/coins to give out
  • Save and Run the program, just to make sure you do not have a syntax error.

Get the original amount

  • Under the #get the initial amount comment, create a variable called amount and use it to store the value of a user input (evaluated as number). This variable should now be a float.
  • Add a print() statement under the #output number of bills comment, and make it print the amount the user wants to withdraw. This is called print()-debugging, and it is helpful in pinning down where things went wrong when we run into bugs.
  • Verify that your program works as expected.

Challenge 1: dollars and cents

Life is a lot easier if we deal with dollars and cents separately:
  • Split the amount into dollars (an int) and cents (a float). Hint: try using the math.floor(...) function to get the dollars and subtraction to get the cents

Challenge 2: $20 bills

When making change, we start from the largest size bill (or coin) and work our way down. In the US currency system, this minimizes the total number of bills/coins needed. In this step, you’ll compute the number of $20 bills necessary:
  • Under the # compute number of bills comment, use the correct operator (//, /, or %) to compute the number of $20 bills and store that value in a new variable, called num_20s.
  • Make your program output the num_20s variable in the output section.
  • Verify that your program works as expected.

Challenge 3: remaining amount

  • Go back to the computation of the number of $20s, and compute the amount of money left over once the $20s are taken out of the amount. There are several ways of doing this: you can do it using the % modulo operator, or using multiplication and subtraction. Whichever method you prefer is fine!
  • Store this amount in a new variable called remainder.
  • Make your program output the remainder, just to make sure that value is computed correctly.
  • Verify that your program works as expected.

Challenge 4: other denominations

  • Now that you have the structure for your program, add enough Python code to make your code display:
    • The number of $20-bills
    • The number of $10-bills
    • The number of $5-bills
    • The number of $1-bills
  • Verify that your program works. Below is a typical output you should try to emulate:

Final Challenge: computing coins

The coins are going to be a little trickier because operations on floats are imprecise. For example, look at what happens when you execute the following in the shell:

import math
amount = 2.19
dollars = math.floor(amount)
cents = amount - dollars
print(cents)

You’ll most likely get something that looks like this: Weird, huh? This is an artifact of the limited precision of storing real numbers in finitely many bits. If you want to read more about the limitations of floating point operations in Python, check out: The Python Tutorial Chapter 15. Floating Point Arithmetic: Issues and Limitations.

For now, we’ll have to find a workaround.
  • Convert the value stored in you cents variable (which is currently a float) to an integer value using a combination of the round(...) function and multiplication by 100.
  • Using the same procedure you discovered to calculate the number of each denomination of bill, figure out how many quarters, dimes, nickels and pennies are needed to make change to this amount.
  • Update your output and check to make sure that the numbers make sense:

Submission of Lab 2

  • Use this link to submit your lab2.py file via Moodle.

Bonus Challenge

Finally, .format() your output so that it is easy to read: