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!
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.
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:
# get the initial amount
# compute number of bills
# compute number of coins
# output number of bills/coins to give out
#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
.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.dollars
(an int
) and cents
(a float
). Hint: try using the math.floor(...)
function to get the dollars
and subtraction to get the cents
# 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
.num_20s
variable in the output section.remainder
.remainder
, just to make sure that value is computed correctly.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.
cents
variable (which is currently a float
) to an integer value using a combination of the round(...)
function and multiplication by 100.lab2.py
file via Moodle.Finally, .format()
your output so that it is easy to read: