1

Trying to use a function to change a from 0 to 1, but this outputs 0:

a = 0

def function():
    a = 1

function()

print (a)
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • There's nothing wrong, you just need to declare `a` as a global variable in the function. – Prateek Dewan May 20 '20 at 18:46
  • 1
    `a` is local to the function when you assign it. `global` is the keyword you are looking for here, but in most cases, if the answer is `global`, you're asking the wrong question. – jordanm May 20 '20 at 18:47
  • define function as `def function(a):` and call it as `function(a)` – Ehsan May 20 '20 at 18:48

0 Answers0