Friday, May 25, 2018

function - Incrementing integer variable of global scope in Python

I am trying to change global value x from within another functions scope as the following code shows,



x = 1
def add_one(x):

x += 1


then I execute the sequence of statements on Python's interactive terminal as follows.



>>> x
1
>>> x += 1
>>> x
2

>>> add_one(x)
>>> x
2


Why is x still 2 and not 3?

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...