I need to create a program that calculates change after a purchase. This will be
ID: 3902189 • Letter: I
Question
I need to create a program that calculates change after a purchase. This will be a sequential algorithm, which is one of the simplest programming patterns. Your solution requires no branching or looping. Simply write a series of statements that will execute in order.
The program should ask for a purchase price and the amount of cash tendered. It should then determine how many of the following denomininations should be returned:
penny -$0.01
nickel - $0.05
dime - $0.10
quarter - $0.25
dollar $1.00
five -$5.00
ten - $10.00
twenty - $20.00
Here is some of the python I have written:
import math
price = float(input("Enter price of item: ")) #taking in price of item
cash = float(input("Enter cash tendered: ")) #taking in cash from user
change = cash - price # calculate change
change = int(change * 100) # multiply range by 100 to get an integer
print("{0:.2f}".format(price))
print("{0:.2f}".format(cash))
print("{0:.2f}".format(change))
quarters = 0 # defining variables for change
dimes = 0
nickels = 0
pennies = 0
dollars = int(change/100) #calculate dollars
fives = int(change/20)
tens = int(change/10)
twenties = int(change/5)
cents = change - (dollars*100)
pennies = cents
cents = cents - (nickels * 5)
nickels = math.trunc(nickels * 5)
dimes = math.trunc(cents/10)
cents = cents - (dimes * 10)
quarters = math.trunc(cents/25)
cents = cents - (quarters * 25)
print("Dollars : " + str(dollars)) #output change
print("Quarters : " + str(quarters))
print("Dimes : " + str(dimes))
print("Nickels : " + str(nickels))
print("Pennies : " + str(pennies))
Explanation / Answer
Given below is the code (tested on python 3.5) for the question. Make sure to indent the code exactly as shown in image
Please don't forget to rate the answer if it was helpful. Thank you
import math
price = float(input("Enter price of item: ")) #taking in price of item
cash = float(input("Enter cash tendered: ")) #taking in cash from user
change = cash - price # calculate change
#change = int(change * 100) # multiply range by 100 to get an integer
print("{0:.2f}".format(price))
print("{0:.2f}".format(cash))
print("{0:.2f}".format(change))
twenties = int(change / 20)
change = change - 20 * twenties
tens = int(change / 10)
change = change - 10 * tens
fives = int(change / 5)
change = change - 5 * fives
dollars = int(change)
change = change - dollars
change = change * 100 #now only cents are left ..mulitply so that conversion is easy
quarters = int(change/25)
change = change - 25 * quarters
dimes = int(change/10)
change = change - 10 * dimes
nickels = int(change/5)
change = change - 5 * nickels
pennies = int(change)
#output change
print("Twenties: " + str(twenties))
print("Tens: " + str(tens))
print("Fives: " + str(fives))
print("Dollars : " + str(dollars))
print("Quarters : " + str(quarters))
print("Dimes : " + str(dimes))
print("Nickels : " + str(nickels))
print("Pennies : " + str(pennies))
output
=====
2.03
100.00
97.97
Twenties: 4
Tens: 1
Fives: 1
Dollars : 2
Quarters : 3
Dimes : 2
Nickels : 0
Pennies : 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.