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,...
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...
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
In a two input scenario, if the price of one input changes,____________ will also change: Group...
In a two input scenario, if the price of one input changes,____________ will also change: Group of answer choices the profit the marginal physical product the average value product all of the above
2) Write an elementary calculator in which the main program reads an integer input, an operation...
2) Write an elementary calculator in which the main program reads an integer input, an operation (+,-,*,/), and another integer input. It then passes this information to a function (calc) to perform this operation. This function (calc) returns the result using switch statements for operation decision. The program stops when the user inputs 0 for both integer inputs in the main program.
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT