1

So I am having trouble getting this system to work, and I can't be sure that I'm asking the right question, but here is what is happening and what I want to happen.

money = 1
def Stats():
     print
     print "money " + str(money)

def gainM():
     money + 2
     Stats()

if money == 1:
     gainM()

Now what happens when it goes to print money, the value is still 1 even though I add 2 to the value. (code is not a copy of my actual program, but an example to show what is happening.)

1
  • money + 2 does not do anything. In any case, neither of the functions have access to money — you should put them in a class or pass money as an argument and return the updated value. Commented Feb 23, 2013 at 1:23

2 Answers 2

6

money + 2 is a no-op. You actually have to assign money to a new value

money = money + 2
# or
money += 2

But then you'll find you get an error - you can't assign to variables outside a function scope. You can use the global keyword:

global money
money += 2

This will allow you to change the value of money within the function.

However, the recommended way is passing money as a parameter:

def gainM(money):
    money += 2
    Stats()
    return money

if money == 1:
    money = gainM(money)

If you're using the second option (which you should be), you also need to change your Stats function, to have a money parameter as well.

def Stats(money):
     print
     print "money " + str(money)

Otherwise the function will print 1 instead of 3.

Another recommendation - use string formatting.

'money %d' % money  # the old way
'money {}'.format(money)  # the new and recommended way

Now you pass money into the Stats function.

def gainM(money):
    money += 2
    Stats(money)
    return money
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the help.. i would like to point out, i originally had it money = money + 2, but i had an error, so i changed it, but didnt think to test if what i was doing actually did anything. thanks.
2

You need to assign the new value to money. Like so:

money = money + 2

Or the shorthand form:

money += 2

Also, if the variable is outside your function, you need to declare it global(so it doesn't instead create a local variable)

So you end up with:

def gainM():
    global money
    money += 2
    Stats()

Edit: just to clarify, I'm not saying you should use global variables. In general they are a bad idea(though they may be useful in some situations). However, that's what this particular example needs to work. Chances are, however, that what you want is probably a class with instance variables that the methods of that class modify. However, given that you don't seem to have grasped the basics of the language yet, take things one step at a time and don't worry about any words in my previous sentences you didn't understand for now :)

2 Comments

Thank you everyone, entropy, u may well be right, i havent grasped the basics, but with how i learn, i like to take things in their stride, I come up with an idea, and as i incounter a problem, i learn how to get past it.
That's definitely the best way I've found to learn new stuff so far. Though at some point, once you grasp the basics, reading about more abstract things like general design patterns that help with managing the complexity of your code is definitely helpful. Good luck to you mate :-)

Your Answer

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.