Goal: write a program to determine how many quarters, dimes, nickels, and pennies make up a given amount of change
Specification: User enters an amount as an integer
Program prints number of quarters, dimes, nickels, and pennies that make up the given amount
High-level design:
read in amount
figure out how may quarters are in the amount
determine how much is left over from this
figure out how many dimes are in what’s left over
determine how much is left over from this
figure out how many nickels are in what’s left over
what’s left is the number of pennies
Part #1: Data Representation
Represent the amount as an integer
Part #2: Program Structure
Part #3: Refine algorithm
This is program change0.py.
# read in the amount of change and make it a number A = input("Amount of change: ") IA = int(A) # how many quarters NQ = IA // 25 # how many dimes in what’s left over IA = IA % 25 ND = IA // 10 # how many nickels in what’s left over IA = IA % 10 NN = IA // 5 # how many pennies in what’s left over IA = IA % 5 print(A, "cents is", NQ, "quarters,", ND, "dimes,", NN, "nickels, and", IA, "pennies")
|
MHI 289I, Programming for Health Informatics Version of September 28, 2020 at 1:04PM
|
You can also obtain a PDF version of this. |