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 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,...
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...
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...
1. Specification Write a C program to implement a simple calculator that accepts input in the...
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...
In the second task, you will write a Java program that validates an input to see...
In the second task, you will write a Java program that validates an input to see whether it matches a specified pattern. You will have to read a bit about regular expressions for this Lab. 1. Prompt the user to enter the email address. 2. Read the user input as a String. 3. A regular expression that specifies the pattern for an email address is given by “[a-zA-Z0- 9.]++@[a-zA-Z]++.(com|ca|biz)”. Use this as the input to the matches method in the...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator,...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator, reading an infix algebraic expression with numbers and simple operations: +, -, *, / , (, and ). The program converts an infix expression into an equivalent postfix expression, and then evaluates the postfix expression, and then prints the result if input expression is correct otherwise prints error messages. Your program must interact with the user until the user quits.    REQUIREMENTS: - Your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT