n
is a local variable in add()
, and as such it is not the same variable as the global n
. The two are (quite deliberately) not connected.
If you want to change the global, you have two options:
reassign the return value from
add()
back to the globaln
:n = add(n)
alter the global directly by marking the name
n
inadd
as a global. Youradd()
function doesn't need to use an argument in that case:def add(): global n n += 1