Please note the individual requirements for each given below. Follow assignment
ID: 3593884 • Letter: P
Question
Please note the individual requirements for each given below. Follow assignment guidelines (do not use multiple return statements in a function, only call main once, etc.). Also do not use lists (or arrays) to complete this lab.
Problem 1: Your program should only have 2 functions: the main function and one other function. Do not use any ifs or loops to complete this program. The main function should open the file, read the amount due and the amount paid, calculate the change due (it will be less than 99 cents), and multiply the change due by 100 to get a number between 0 and 99 inclusive. Send this number to the function. Once the function is done the main function should output the amount due, the amount paid, number of quarters, dimes, nickels and pennies to be given back. The second function should accept the change that is due and calculate the number of quarters, dimes, nickels, and pennies needed using minimum number of operators (no loops or ifs should be used). The number of quarters, dimes, nickels, and pennies should be sent back to main for output.
Explanation / Answer
def getChangeDue(filename):
changeDue = 0
with open(filename) as fh:
amountDue, amountPaid = fh.readlines()
amountDue = float(amountDue)
amountPaid =float(amountPaid)
changeDue = 100*(amountDue-amountPaid)
return changeDue
def getQuatersDimesNickelsPennies(change):
quaters = change//25
change = change % 25
dimes = change//10
change = change%10
nickels = change//5
pennies = change//5
return (quaters, dimes, nickels, pennies)
def main():
changeDue = getChangeDue('amount.txt')
(quaters, dimes, nickels, pennies) = getQuatersDimesNickelsPennies(changeDue)
print("Quaters: %d, Dimes: %d, Nickels: %d, Pennies: %d" % (quaters, dimes, nickels, pennies))
I have assumed that data is in file amount.txt and is of form
1000.00
999.07
Sample run
Quaters: 3, Dimes: 1, Nickels: 1, Pennies: 1
# copy pastable code link: https://paste.ee/p/2WmD7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.