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()
Get Answers For Free
Most questions answered within 1 hours.