-3

For some reason Money doesn't change to 8 as it should; it always stays at 10.

Money = 10
Resto = 0
ApplePrice = 2


def buy(current, price):
    Money == current - price
    return Money

buy(Money, ApplePrice)
print(Money)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Narkem
  • 103
  • 2
  • 4
    Money in your function knows nothing about the Money outside – Padraic Cunningham Dec 31 '15 at 19:53
  • @PadraicCunningham Agreed that the dupe is right, but just another thing, it should be this: `Money = current - price` which may confuse the OP. – Zizouz212 Dec 31 '15 at 19:55
  • Your function has a return value ... you should set a variable for accept that return value – Arash Hatami Dec 31 '15 at 19:56
  • `Money == current - price` is wrong from two perspectives. One is the use of == vs =. Your function drops the value of `Money == current-price`(a boolean) on the bit floor. Even if you fix that, it will not do what you think it should do. – David Hammen Dec 31 '15 at 19:56
  • @Zizouz212. True but that is most likely a typo, changing to `=` is not going to change the Money variable outside the function which is what the OP expects – Padraic Cunningham Dec 31 '15 at 19:57
  • @CedricH., it is 100 percent about the scope of the variables. – Padraic Cunningham Dec 31 '15 at 19:57
  • 1
    @CedricH. This has everything to do with globals... If there was an understand of that, then the OP would've figured this out already. Padraic, true. You've got a point. – Zizouz212 Dec 31 '15 at 19:58

1 Answers1

0

Rather than altering the global variable I'd recommend keeping your variables as they are & using the return value from buy();

money = buy(MONEY, APPLEPRICE)
print(money)

You've also got a problem with the calculation in the function.

You would want this, defining your constants first.

MONEY = 10
RESTO = 0
APPLEPRICE = 2

def buy(current, price):
    money = current - price
    return money

money = buy(MONEY, APPLEPRICE)
print(money)

I know docs aren't that interesting, but take a look over PEP8 as it will help you write good code to a standard most of us try to match.

markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • This is wrong. Look at the first line of the `buy` function. You're evaluating an expression, not an assignment (which I assume is what you're trying to do. This won't solve anything. – Zizouz212 Dec 31 '15 at 19:58
  • @Zizouz212 that doesn't make my original point wrong. It just means there's more than one problem. – markwalker_ Dec 31 '15 at 19:59
  • 1
    Seriously, please read the code. – Zizouz212 Dec 31 '15 at 19:59
  • 1
    Using `MONEY` as a global variable whose value changes goes very much against the grain of PEP8. A thing with an all-caps name (e.g., `CONSTANT`, or `MONEY`) should be constant! – David Hammen Dec 31 '15 at 20:03