In: Computer Science
Change Calculator
Task:
Create a program that will input a price and a money amount. The program will then decide if there is any change due and calculate how much change and which coins to hand out. Coins will be preferred in the least number of coins (starting with quarters and working your way down to pennies).
Input:
total_price, amount_tender
allow the user to input 'q' for either value if they want to quit
the program
Validate:
total_price
amount_tender
Output:
change_amount
number_quarters
number_dimes
number_nickels
number_pennies
IF any of these values is zero, DO NOT output that value. IE: if the change is 26 cents you only need to tell the user they will receive 1 quarter and 1 penny. No more, no less.
Hint:
Use modular division to track the remainder after calculating each number of coins.
Convert the decimal to a whole number to make the calculation easier. Use math.floor() to round DOWN to the nearest integer.
Use a try-catch (try-except) to help validate input. Try to set the input value into afloat and if it does not fit then you can test the input further without an error stopping your program.
Requirements:
Use a function
Use a loop
Validate all input
Use an IF statement
Use helpful, human-readable text both to prompt for input, but also to let the user know about invalid inputs. Simply stating it is invalid doesn't help the user to correct their error.
If you can make your program also calculate the number of dollars for change. That is, change above 99 cents.
Turn IN:
Python file ONLY
In case of any queries,please comment. I would be very happy to assist all your queries.Please give a Thumps up if you like the answer.
Program
import sys
def calcCoins(moneyback):
quarter = 0.25
dime = 0.10
nickel = 0.05
penny = 0.01
moneybacknew=moneyback-int(moneyback)
quarter_money_back = int(moneybacknew / quarter)
partialtotal = moneybacknew - quarter_money_back * quarter
dimes_money_back = int(partialtotal / dime)
dpartialtotal = partialtotal - dimes_money_back * dime
nickel_money_back = int(dpartialtotal / nickel)
npartialtotal = dpartialtotal - nickel_money_back * nickel
penny_money_back = int(npartialtotal / penny)
penny_partial_total = npartialtotal - penny_money_back * penny
return
quarter_money_back,dimes_money_back,nickel_money_back,penny_money_back
def calcDollar(moneyback):
hundred_dollar_bill = 100
fifty_dollar_bill = 50
twenty_dollar_bill = 20
ten_dollar_bill = 10
five_dollar_bill = 5
two_dollar_bill = 100
one_dollar_bill = 1
fiftydmb = int(moneyback / fifty_dollar_bill)
fiftydnewbalance=moneyback-fiftydmb*fifty_dollar_bill
twentydmb=int(fiftydnewbalance / twenty_dollar_bill)
twentydnewbalance=fiftydnewbalance-twentydmb*twenty_dollar_bill
tendmb=int(twentydnewbalance / ten_dollar_bill)
tendnewbalance=twentydnewbalance-tendmb*ten_dollar_bill
fivedmb=int(tendnewbalance / five_dollar_bill)
fivednewbalance=tendnewbalance-fivedmb*five_dollar_bill
twodmb= int(fivednewbalance / two_dollar_bill)
twodnewbalance=fivednewbalance-twodmb*two_dollar_bill
onedmb=int(twodnewbalance / one_dollar_bill)
onednewbalance=twodnewbalance-onedmb*one_dollar_bill
return fiftydmb,twentydmb,tendmb,fivedmb,twodmb,onedmb
def main():
while True:
try:
total_price = input("Enter the price of item: $ ")
if (str(total_price)=='q'):#user want to quit the program
print("Quit!!!")
sys.exit(0)
total_price=float(total_price)
if (total_price<=0):#total_price cannot be negative and cannot
be 0
print("Sorry, Total price must not be negative or zero.")
continue
except ValueError:
print("Sorry, Enter valid amount")
continue
else:
break
while True:
try:
amount_tender = input("Enter how much money given: $")
if (str(amount_tender)=='q'):#user want to quit the program
print("Quit!!!")
sys.exit(0)
amount_tender=float(amount_tender)
if (amount_tender<=total_price):#amount_tender cannot be less
than total_price or can be equal to total_price
print("Sorry,Amount Tender cannot be less than or equal to Total
Price")
continue
except ValueError:
print("Sorry, Enter valid amount")
continue
else:
break
change_amount = amount_tender - total_price
fiftydmb,twentydmb,tendmb,fivedmb,twodmb,onedmb=calcDollar(change_amount)
number_quarters,number_dimes,number_nickels,number_pennies=calcCoins(change_amount)
print("Change Due : $",change_amount)
print("Balance to be given : ")
if fiftydmb>0:
print (fiftydmb,"$ 50 Bill")
if twentydmb>0:
print (twentydmb,"$ 20 Bill")
if tendmb>0:
print (tendmb,"$ 10 Bill")
if fivedmb>0:
print (fivedmb,"$ 5 Bill")
if twodmb>0:
print (twodmb,"$ 2 Bill")
if onedmb>0:
print (onedmb,"$ 1 Bill")
if number_quarters>0:
print(number_quarters, 'quarters' )
if number_dimes>0:
print(number_dimes,'dime')
if number_nickels>0:
print(number_nickels,'nickels')
if number_pennies>0:
print(number_pennies, 'pennys')
main()
Output
Enter the price of item: $ q
Quit!!!
>>>
Enter the price of item: $ 0
Sorry, Total price must not be negative or zero.
Enter the price of item: $ -9
Sorry, Total price must not be negative or zero.
Enter the price of item: $ w
Sorry, Enter valid amount
Enter the price of item: $ q
Quit!!!
>>>
Enter the price of item: $ 9
Enter how much money given: $q
Quit!!!
>>>
Enter the price of item: $ 9
Enter how much money given: $9
Sorry,Amount Tender cannot be less than or equal to Total
Price
Enter how much money given: $4
Sorry,Amount Tender cannot be less than or equal to Total
Price
Enter how much money given: $100
Change Due : $ 91.0
Balance to be given :
1 $ 50 Bill
2 $ 20 Bill
1 $ 1 Bill
Enter the price of item: $ 0
Sorry, Total price must not be negative or zero.
Enter the price of item: $ -9
Sorry, Total price must not be negative or zero.
Enter the price of item: $ 9.74
Enter how much money given: $8
Sorry,Amount Tender cannot be less than or equal to Total
Price
Enter how much money given: $0
Sorry,Amount Tender cannot be less than or equal to Total
Price
Enter how much money given: $100
Change Due : $ 90.26
Balance to be given :
1 $ 50 Bill
2 $ 20 Bill
1 quarters
1 pennys
Program Screenshot