0

I'm calculating the minimum coinage of change in Python using the following code:

def change(am):
 totalcoins = []
 for coin in dstock:
 while coin[0] <= am and coin[1] > 0:
 totalcoins.append(coin[0])
 am = round(am - coin[0], 2)
 coin[1] = coin[1] - 1
 return totalcoins

dstock is a variable created from a CSV file. Here is the print of dstock:

[[1.0, 100], [0.5, 100], [0.2, 100], [0.1, 100], [0.05, 100], [0.02, 100], [0.01, 100]]

Explained: [denomination,stock level] -- at present there are 100 of each.

I have figured how to update the stock level and how to calculate the change but I can't figure out how to produce an error if there is not enough stock to produce the change.

example:

Stock = [[1.0, 0], [0.5, 0], [0.2, 0], [0.1, 0], [0.05, 0], [0.02, 0], [0.01, 2]]
change = 0.03

if stock < change print(error: not enough stock to produce change)

Buzz
1,91721 silver badges26 bronze badges
asked Jan 5, 2017 at 14:28
4
  • start with this: sum(a*b for (a,b) in Stock) Commented Jan 5, 2017 at 14:31
  • 1
    Sorry, I'm very new to this. What will this do? Commented Jan 5, 2017 at 14:31
  • 1
    a*b for (a, b) in Stock will multiply the sublists together (1.0 * 0, 0.01 * 2, etc) and sum will take those values and add them together. Commented Jan 5, 2017 at 14:35
  • this will sum products of all denomination, so basically you get the amount to compare with change. If you really need to throw an error, then you should implement exception otherwise you can just check two numbers and print message if condition is not met Commented Jan 5, 2017 at 14:35

2 Answers 2

2

No additional loop.

Edited to accommodate the update of stock is only allowed if there is enough change.

def change(am, dstock):
 totalcoins = []
 hstock=dstock[:] #make a copy of dstock
 for coin in hstock:
 while coin[0] <= am and coin[1] > 0:
 totalcoins.append(coin[0])
 am = round(am - coin[0], 2)
 coin[1] = coin[1] - 1
 if am>0:
 raise ValueError("not enough stock to produce change")
 else:
 dstock = hstock
 return totalcoins
dstock = [[1.0, 0], [0.5, 0], [0.2, 0], [0.1, 0], [0.05, 0], [0.02, 0], [0.01, 3]]
print change(0.03, dstock) #[0.01, 0.01, 0.01]
print dstock #[[1.0, 0], [0.5, 0], [0.2, 0], [0.1, 0], [0.05, 0], [0.02, 0], [0.01, 0]]
answered Jan 5, 2017 at 14:45
Sign up to request clarification or add additional context in comments.

2 Comments

THANKYOU! This works great for producing the error but it does not return the coins removed. coin[1] = coin[1] - 1 is still completed. is there anyway to return these to the stock if there isn't enough to produce the change?
Thanks for adding this, it produced an error for me? UnboundLocalError: local variable 'dstock' referenced before assignment
1

Your question is quite ambiguous, but I take it that you are asking how to produce an error. If not, please elaborate

Use an exception and a conditional:

if stock < stock_required_for_change:
 raise Exception("Not enough Stock to produce change")
answered Jan 5, 2017 at 14:32

2 Comments

Thanks. Each coin has a stock level i.e 0.01, has a stock level of 2. If the change required from a calculation is 0.03 there is not enough stock of 0.01 to provide the change, therefore it would have to produce an error. Does that make sense?
@JAM Are you asking how to raise an exception or how to determine whether a certain change value can be made from a given stock? If the latter, that's not really a programming issue and is a little out of scope here.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.