0

I want to be able to work out how much change in coins is needed for each amount I enter, EG (1.74 would be 1 £1, 1 50p, 1 20p and 4 1ps). This was going to be the function to work out the 20ps necessary. I know I could iterate through a list and it would make the code below easier and neater but just wanted to see if this way would work as it was how I first thought to do it.


def num_20ps(x):
    pounds = x // 1
    num1 = x - pounds
    if num1 >= 0.50:
        n50 = num1 / 0.50
        n50s = int(n50)
        num2 = num1 - (0.5 * n50s)
        if num2 >= 0.20:
            n20 = num2 // 0.20
            return n20


print(num_20ps(1.7))

What I cant understand is why when I print my num2 variable I get an output of 0.19999999999999996. Where did the 0.00000000000000004 randomly go?

Nathan
  • 9,651
  • 4
  • 45
  • 65
Dan
  • 79
  • 3

0 Answers0