Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #! python3
  2. import math
  3.  
  4. cost = int(float(input("How much did the item cost?"))*100)
  5. moneyPaid = int(float(input("How much money did you pay?"))*100)
  6.  
  7. moneyTypes = {"pennies": 1, "nickels": 5, "dimes": 10, "quarters": 25,
  8. "$1 bills": 100, "$5 bills": 500, "$10 bills": 1000,
  9. "$20 bills": 2000, "$50 bills": 5000, "$100 bills": 10000}
  10. amountTypes = {"pennies": 0, "nickels": 0, "dimes": 0, "quarters": 0,
  11. "$1 bills": 0, "$5 bills": 0, "$10 bills": 0,
  12. "$20 bills": 0, "$50 bills": 0, "$100 bills": 0}
  13.  
  14. def printResult(itemsDict, lWidth, rWidth):
  15. print("")
  16. print("CHANGE RECEIVED".center(lWidth+rWidth, "-"))
  17. print("")
  18. for k, v in amountTypes.items():
  19. print(k.ljust(lWidth, ".") + str(v).rjust(rWidth))
  20. print("You have received ", end = "")
  21. for k, v in amountTypes.items():
  22. if v > 0:
  23. print(str(v) + " " + k + ", ", end = "")
  24.  
  25. def getTheDifference(myCost, myMoneyPaid):
  26. validTypes = []
  27. lengthList = []
  28. difference = myMoneyPaid - myCost
  29. for price in moneyTypes.values():
  30. if difference >= price:
  31. validTypes.append(price)
  32. validTypes.sort(reverse = True)
  33.  
  34. for price in validTypes:
  35. howManyType = math.floor(difference/price)
  36. amountTypes[list(moneyTypes.keys())[list(moneyTypes.values()).index(price)]] = howManyType #issue here
  37. difference = difference - howManyType*int(price)
  38. if difference == 0:
  39. break
  40.  
  41. printResult(moneyTypes, 10, 10)
  42.  
  43.  
  44. while cost > moneyPaid:
  45. print("You did not pay enough money")
  46. moneyPaid = int(float(input("How much money did you pay?"))*100)
  47.  
  48. if cost == moneyPaid:
  49. print("No change returned")
  50. elif cost < moneyPaid:
  51. getTheDifference(cost, moneyPaid)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement