The original version was written by R. Jordan Crouser at Smith College, and it has been updated in January 2019/2020 by Alicia M. Grubb.
If you have not already done so, pick up your name card and find a new partner.
Before begining the lab, take 2-3 minutes and get to know your lab partner. Do you like pets (cat/dogs/others)? What was the most recent movie you saw in theatres?
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 in Thonny (bottom window) and type the expressions below. Together, 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!
Now that you have started to understand, we will move onto the portion of the lab that you will hand in. Begin by downloading the lab starter file (click this link), and save it locally.
Update the header with the relavent information. Here is an example header:
# ------------------------------------------------------
# Name: Ada Lovelace and Katherine Johnson
# Section: L02
# Filename: lab1p1.py
# Peers: Grace Hopper
# References: N/A
# ------------------------------------------------------
Write a small program (either in the Python Shell, or in the Edit window), that asks the user to enter two integers (recall the input command), 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
What happens if the second number is bigger than the first number? What happens if you enter zero for one of the numbers? Create a few “test cases” to make sure that your code works in all situations.
At this point you should switch driver/navigator roles.
Begin by downloading the second starter file (click this link), and save it locally. Update the header with the relavent information.
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
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.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
. You can also use the integer division //
and modulo %
functions.
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:
# 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.At this point you should switch driver/navigator roles.
remainder
.remainder
, just to make sure that value is computed correctly.Verify that your program works. Below is a typical output you should try to emulate:
Try at least five different values for amount. Include a comment ##
in your code, listing the values that you tested.
At this point you should switch driver/navigator roles.
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:
0.1899999999999999995
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.
cents
variable (which is currently a float
) to an integer value using a combination of the round(...)
function and multiplication by 100.At this point you should switch driver/navigator roles.
We just learned about the limited precision of storing real numbers in finitely many bits. Take 2 minutes and discuss with your partner how this may pose a risk or issue in the real world. Have you encountered any “rounding errors” in your life?
Begin by downloading the third starter file (click this link), and save it locally. Update the header with the relavent information.
At Smith College, the grade breakdown is as follows:
A denotes the grade 90 - 100.
B denotes the grade 80 - 89.
C denotes the grade 70 - 79.
D denotes the grade 60 - 69.
E denotes the grade 0 - 59.
Write a program that takes as input a number and outputs if the grade is a pass.
Update your program to take as input a number and outputs if the grade is an S (Satisfactory) or a U (Unsatisfactory). For this example, we will say that 75 or above is S.
Update your program to take as input a number and outputs the letter grade.
Update your program to take as input either a letter grade or a number grade and converts it to the opposite. You can start from the program we wrote in class but this is insufficient.
lab1p1.py
, lab1p2.py
, and lab1p3.py
files before the end of the day.