1

So guys, I have a funky error going on and I'm wondering if you could help me out. I have a function that's supposed to find the way to give change using the least coins.

def change_counter(cost, paid):
    changefor = paid - cost

and i have a a variable, changefor which is basically how much money you need to make change for. Then I declared some variables, penny = 0 and such that track how many of each coin are being given out. Then I made a while loop, that cycles until changefor hits zero.

while(changefor > 0):
    if changefor >= 100:
        changefor - 100
        ++hundreddollar
    elif,etc

The rest is really self explanatory, and just printing the amounts of each coin used, etc. Does anyone see what could be causing a problem? I get a return value of -1, and nothing from the while loop seems to be doing anything.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173

1 Answers1

2

Because you're computing things but not storing them anywhere

First, thank you for teaching me something new.

++x

does compile, and also does nothing. Look here, Why are there no ++ and --​ operators in Python?, but there are no increment/decrement operators in python. Similarly,

changefor - 100

Doesn't do anything. Sure, it evaluates changefor and subtracts 100, but then it doesn't store that value anywhere. Pitfall of a dynamic language.

changefor = changefor - 100

or

changefor -= 100 

will do you better

Edit: I was about to ask why the decrement syntax is accepted and found that this person Behaviour of increment and decrement operators in Python already ran into this oddity.

Community
  • 1
  • 1
en_Knight
  • 5,301
  • 2
  • 26
  • 46