money_machine.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. class MoneyMachine:
  2. CURRENCY = "$"
  3. COIN_VALUES = {
  4. "quarters": 0.25,
  5. "dimes": 0.10,
  6. "nickles": 0.05,
  7. "pennies": 0.01
  8. }
  9. def __init__(self):
  10. self.profit = 0
  11. self.money_received = 0
  12. def report(self):
  13. """Prints the current profit"""
  14. print(f"Money: {self.CURRENCY}{self.profit}")
  15. def process_coins(self):
  16. """Returns the total calculated from coins inserted."""
  17. print("Please insert coins.")
  18. for coin in self.COIN_VALUES:
  19. self.money_received += int(input(f"How many {coin}?: ")) * self.COIN_VALUES[coin]
  20. return self.money_received
  21. def make_payment(self, cost):
  22. """Returns True when payment is accepted, or False if insufficient."""
  23. self.process_coins()
  24. if self.money_received >= cost:
  25. change = round(self.money_received - cost, 2)
  26. print(f"Here is {self.CURRENCY}{change} in change.")
  27. self.profit += cost
  28. self.money_received = 0
  29. return True
  30. else:
  31. print("Sorry that's not enough money. Money refunded.")
  32. self.money_received = 0
  33. return False