# Listing what coins are needed for given amount python

I need to write a function that prints the amount of UK coins needed for a given amount, in a list format (i.e. 8 values in the list for Â£2 coins, Â£1 coins, Â£0.50, Â£0.20, Â£0.10, Â£0.05, Â£0.02, Â£0.01).

I have so far written the following:

```
def pay_with_coins( amount ):
coins_list = [0, 0, 0, 0, 0, 0, 0, 0]
if amount == 0:
return(coins_list)
else:
while amount > 2.00:
coins_list[0] = (coins_list[0] + 1)
amount = amount - 2.00
while amount >= 1.00 and amount < 2.00:
coins_list[1] = (coins_list[1] + 1)
amount = amount - 1.00
while amount >= 0.50 and amount < 1.00:
coins_list[2] = (coins_list[2] + 1)
amount = amount - 0.50
while amount >= 0.20 and amount < 0.50:
coins_list[3] = (coins_list[3] + 1)
amount = amount - 0.20
while amount >= 0.10 and amount < 0.20:
coins_list[4] = (coins_list[4] + 1)
amount = amount - 0.10
while amount >= 0.05 and amount < 0.10:
coins_list[5] = (coins_list[5] + 1)
amount = amount - 0.05
while amount >= 0.02 and amount < 0.05:
coins_list[6] = (coins_list[6] + 1)
amount = amount - 0.02
while amount >= 0.01 and amount < 0.05:
coins_list[7] = (coins_list[7] + 1)
amount = amount - 0.01
return(coins_list)
```

I am testing the function by passing the following:

```
print(pay_with_coins(0.08))
print(pay_with_coins(8.02))
print(pay_with_coins(1.74))
print(pay_with_coins(1001))
```

This is what I'm supposed to get:

```
[0,0,0,0,0,1,1,1]
[4,0,0,0,0,0,1,0]
[0,1,1,1,0,0,2,0]
[500,1,0,0,0,0,0,0]
```

And this is what I actually get:

```
[0, 0, 0, 0, 0, 1, 1, 0]
[4, 0, 0, 0, 0, 0, 0, 1]
[0, 1, 1, 1, 0, 0, 1, 1]
[500, 1, 0, 0, 0, 0, 0, 0]
```

As you can see, the last two values in the list is where it seems to mess up and I'm not quite sure what the issue is.

I have a feeling that the last two values are messing up because they're 0.05 and 0.01 (2 decimal places). Any idea how to sort that out?

### 1 Answer

What you're looking for, is a way to divide a number into smaller and smaller numbers. one way do do that is via `divmod`

```
def pay_with_coins(amount):
twoer,rest=divmod(amount, 2)
onner,rest=divmod(rest, 1)
halfer,rest=divmod(rest, 0.5)
fifther,rest=divmod(rest, 0.20)
tenther,rest=divmod(rest, 0.10)
twenthier,rest=divmod(rest, 0.05)
fifthier,rest=divmod(rest, 0.02)
hundreder,rest=divmod(rest,0.01)
return [twoer, onner, halfer, fifther, tenther, twenthier,fifthier, hundreder]
```

In the above code, i use `divmod`

continually on the same variable, to separate the values into lower and lower denominations.

Your last condition is not correct:

`amount >= 0.01 and amount < 0.01:`