Question

In: Computer Science

Change Calculator Task: Create a program that will input a price and a money amount. The...

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

  • cannot be negative
  • cannot be 0
  • cannot be text (other than q for quit)

amount_tender

  • cannot be less than total_price
  • can be equal to total_price
  • cannot be text (other than q for quit)

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

Solutions

Expert Solution

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


Related Solutions

Write a program with total change amount as an integer input, and output the change using...
Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes c++ please
Write a program to calculate the amount of money in an account: The user will input...
Write a program to calculate the amount of money in an account: The user will input initial deposit and the number of years to leave that money in the account. The interest is constant at 0.5% for the entire length of time. The output will be the balance, with interest compounded annually. (simple interest formula: interest = principal * rate) What variables will you need to complete the program? What “controls” will you need for the program? What formula will...
Write a program in pyton with total change amount as an integer input, and output the...
Write a program in pyton with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes Can you...
Modify the provided code to create a program that calculates the amount of change given to...
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount. Use three functions: • bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false. • float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal,...
Flooring Quote Calculator Create a program that calculates a price quote for installing different types of...
Flooring Quote Calculator Create a program that calculates a price quote for installing different types of flooring in a house. Allows the user to enter the homeowner’s information, number of rooms where they want new flooring installed, the area of each room, and the type of flooring for each room. Your program will then calculate and print the installation costs. Need to use a House class that contains information about the home and home owner. An inner class, Room, will...
I need someone to create a program for me: Create a program that takes as input...
I need someone to create a program for me: Create a program that takes as input an employee's salary and a rating of the employee's performance and computes the raise for the employee. The performance rating here is being entered as a String — the three possible ratings are "Outstanding", "Acceptable", and " Needs Improvement ". An employee who is rated outstanding will receive a 10.2 % raise, one rated acceptable will receive a 7 % raise, and one rated...
In this project you will create a basic console based calculator program. The calculator can operate...
In this project you will create a basic console based calculator program. The calculator can operate in two modes: Standard and Scientific modes. The Standard mode will allow the user to perform the following operations: (+, -, *, /) add, subtract, multiply, and divide The Scientific mode will allow the user to perform the same functionality as the Standard add, subtract, multiply, and divide (+, -, *, / ) plus the following: sin x, cos x, tan x. (sin x,...
using C language Create a bitwise calculator that ask user for input two input, first int,...
using C language Create a bitwise calculator that ask user for input two input, first int, bitwise operation, second int (i.e 5 & 9) only those bitwise operation are allowed: & ~ ^ | << >>. If user uses wrong operators stop program and ask again. Convert the first int and second int into 8 BITS binary (00000000) and use bitwise operator given by user to either AND, OR, XOR, etc (ie 1001 & 1111)
Your Task: Write a calculator Program with following functions (lack of function will result in a...
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1 - Add 2 - Subtract 3 - Multiply 4 - Divide 5 - Raise X to the power Y 6 - Finds if a number is even or odd 0 - Quit...
Your Task: Write a calculator Program with following functions (lack of function will result in a...
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1 - Add 2 - Subtract 3 - Multiply 4 - Divide 5 - Raise X to the power Y 6 - Finds if a number is even or odd 0 - Quit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT