Advertisement
Guest User

Coding a change calculator

a guest
Jan 29th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1.  
  2. from decimal import * #I use the decimal format to ensure that the money is handled properly
  3. getcontext().prec = 2 #This tells the program how many decimal places to put on the end of things
  4.  
  5.  
  6. out20P=0 #Setting all my variables up
  7. out10P=0
  8. out5P=0
  9. out2P=0
  10. out1P=0
  11. out50p=0
  12. out20p=0
  13. out10p=0
  14. out5p=0
  15. out2p=0
  16. out1p=0
  17. left=0
  18. bill=1
  19. payment=0
  20.  
  21.  
  22. while payment<bill: #I use this loop to make sure the user has enough money
  23. #if they do not the program will ask for data again
  24. bill=Decimal(input("Enter the bill that needs to be paied"))
  25. payment=Decimal(input("Enter how much you paied in pounds and pence"))
  26. if payment<bill: #This is for if the user has not got enough money
  27. print("Invalid funds")
  28. left=payment-bill
  29.  
  30.  
  31.  
  32. while left !=Decimal(0.00):
  33. print(left)
  34. if left>= 20: #If there is more than a £20 difference
  35. left-=20
  36. out20P+=1
  37. elif left>= 10: #If there is more than a £10 difference
  38. left-=10
  39. out10P+=1
  40. elif left>=5: #If there is more than a £5 difference
  41. left-=5
  42. out5P+=1
  43. elif left>=1: #If there is more than a £1 difference
  44. left-=1
  45. out1P+=1
  46. print(left)
  47. elif left>= 0.5: #If there is more than a 50p difference
  48. left-=Decimal(0.50)
  49. out50p+=1
  50. elif left>=0.2: #If there is more than a 20p difference
  51. left-=0.20
  52. out20p+=1
  53. elif left>=0.1: #If there is more than a 10p difference
  54. left-=0.10
  55. out10p+=1
  56. elif left>=0.05: #If there is more than a 5p difference
  57. left-=0.05
  58. out5p+=1
  59. elif left>= 0.02: #If there is more than a 2p difference
  60. left-=0.02
  61. out2p+=1
  62. elif left>= 0.01: #If there is more than a 1p difference
  63. left-=0.01
  64. out1p+=1
  65.  
  66. else:
  67.  
  68. print ("You should have",out20P," of £20s")
  69. print ("You should have",out10P," of £10s")
  70. print ("You should have",out5P," of £5s")
  71. print ("You should have",out2P," of £2s")
  72. print ("You should have",out1P," of £1s")
  73. print ("You should have",out50p," of 50ps")
  74. print ("You should have",out20p," of 20ps")
  75. print ("You should have",out10p," of 10ps")
  76. print ("You should have",out5p," of 5ps")
  77. print ("You should have",out2p," of 2ps")
  78. print ("You should have",out1p," of 1ps")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement