-4

I'm new to learning python, and this has been troubling me for a while, any help would be much appreciated! I would like it to create a list of increasing numbers. (I know an easier way of doing this, but this is a simplified example of the problem in another code where I can't use any simpler method.)

The code;

increase = 0

def adder(number):

    number+=1
    return number

while True:
    adder(increase)
    print(increase)
Andy
  • 49,085
  • 60
  • 166
  • 233
Thomas P G
  • 37
  • 3
  • Why not use a list comprehension or `for` loop? – kylieCatt Jul 11 '14 at 17:53
  • I want the value of the parameter to change, allowing the new value to be called again in the function later. Would reassigning to the global help with that? If so, how would I do that? Thank you! – Thomas P G Jul 11 '14 at 17:56

2 Answers2

2

When you say

number += 1

you are re-assigning number rather than modifying it's existing value. it's as if you did

number = number + 1

If you want to modify the global, you will need to store it back in when calling the function

increase = adder(increase)

at this point you can also simplify the adder function a bit

def adder(number):
    return number + 1

If, however, you want a function to modify a global like that, it will have to do so explicitly. But if this is what you want you should rethink your program:

increase = 0

def adder():
    global increase
    increase += 1

while True:
    adder()
    print(increase)
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
1

You don't use the returned value.

increase = adder(increase)
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176