Question

Python write a class that takes in two arguments (one for the total cost and one...

Python

write a class that takes in two arguments (one for the total cost and one for the amount paid) into a cash register that will give you the most favorable change for an items cost. this class should have a method called most_favorable_change which outputs a string with the most favorable output. must account for singular and plurals (quarters, dimes, pennies, bills). Looking for the least amount of change that is possible.

example:

change_giver = ChangeGiver(62.13, 100)
change_giver.most_favorable_change()

output:
“The least amount of change for an thing that costs $52.13 with an amount paid of $100 is 1 $20 bill, 1 $10 bill, 1 $5 bill, 2 $1 bills, 3 quarters, 1 dime, and 2 pennies.”

Homework Answers

Answer #1
class ChangeGiver:
    def __init__(self, cost, paid):
        self.cost = cost
        self.paid = paid

    def most_favorable_change(self):
        change = (self.paid * 100 - self.cost * 100)
        bills = ['$20 bill', '$10 bill', '$5 bill', '$1 bill', 'quarter', 'dime', 'nickel', 'penny']
        values = [2000, 1000, 500, 100, 25, 10, 5, 1]
        result = []
        for i in range(len(values)):
            if change >= values[i]:
                result.append(int(change // values[i]))
                change %= values[i]
            else:
                result.append(0)
        result_string = []
        for i in range(len(result)):
            if result[i] > 0:
                if result[i] == 1:
                    result_string.append("{} {}".format(result[i], bills[i]))
                else:
                    if values[i] == 1:
                        result_string.append("{} pennies".format(result[i]))
                    else:
                        result_string.append("{} {}s".format(result[i], bills[i]))
        answer = "The least amount of change for an thing that costs ${:.2f} with an amount paid of ${} is {}.".format(
            self.cost, self.paid, ', '.join(result_string))
        if ',' in answer:
            index = answer.rindex(',')
            answer = answer[0:index] + ', and' + answer[index+1:]
        print(answer)


change_giver = ChangeGiver(62.13, 100)
change_giver.most_favorable_change()
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
USE PYTHON Please!!! 4.15 LAB: Exact change Write a program with total change amount as an...
USE PYTHON Please!!! 4.15 LAB: Exact change 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...
javascript 1.Write a function delay that accepts two arguments, a callback and the wait time in...
javascript 1.Write a function delay that accepts two arguments, a callback and the wait time in milliseconds. Delay should return a function that, when invoked waits for the specified amount of time before executing. HINT - research setTimeout(); 2.Create a function saveOutput that accepts a function (that will accept one argument), and a string (that will act as a password). saveOutput will then return a function that behaves exactly like the passed-in function, except for when the password string is...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter feeds. At a high level, your program is going to perform the following: Read in two files containing twitter feeds. Merge the twitter feeds in reverse chronological order (most recent first). Write the merged feeds to an output file. Provide some basic summary information about the files. The names of the files will be passed in to your program via command line arguments. Use...
public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer>...
public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer> moneyBox; //store the log of transactions for auditing StringBuilder log; /** * Constructs an empty register */ public SimpleRegister() { moneyBox = new TreeMap<Integer, Integer>(); moneyBox.put(1, 0); moneyBox.put(5, 0); moneyBox.put(10, 0); moneyBox.put(25, 0); moneyBox.put(100, 0); moneyBox.put(500, 0); moneyBox.put(1000, 0); log = new StringBuilder(); } @Override public void addPennies(int num) { moneyBox.put(1, moneyBox.get(1) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 1 / 100.0f);...
Design a FSM for a Vending Machine In this task, you will design a FSM for...
Design a FSM for a Vending Machine In this task, you will design a FSM for a simple (albeit strange) vending machine of office supplies. The vending machine sells three possible items, each at a different cost: Item Cost Pencil 10 cents Eraser 20 cents Pen 30 cents The vending machines accepts nickels (worth 5 cents), dimes (worth 10 cents), and quarters (worth 25 cents). Physically, it is only possible to insert a single coin at a time. The vending...
***Programming language is Java. After looking at this scenario please look over the requirements at the...
***Programming language is Java. After looking at this scenario please look over the requirements at the bottom (in bold) THIS IS ALL THAT WAS PROVIDED. PLEASE SPECIFY ANY QUESTIONS IF THIS IS NOT CLEAR (don't just say more info, be specific)*** GMU in partnership with a local sports camp is offering a swimming camp for ages 10-18. GMU plans to make it a regular event, possibly once a quarter. You have been tasked to create an object-oriented solution to register,...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation....
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation. case:    W17400 APIGEE: PEOPLE MANAGEMENT PRACTICES AND THE CHALLENGE OF GROWTH Ranjeet Nambudiri, S. Ramnarayan, and Catherine Xavier wrote this case solely to provide material for class discussion. The authors do not intend to illustrate either effective or ineffective handling of a managerial situation. The authors may have disguised certain names and other identifying information to protect confidentiality. This publication may not be...