0

I wrote a simple script which increments a integer, but it is not working and i got an error.

my script :

x = 1
def inc():
    x +=1
    print(x)

if __name__ == "__main__""" :
    inc()

when i run this script i got this :

palash@ash:~/PycharmProjects/src$ python tt.py
Traceback (most recent call last):
  File "tt.py", line 7, in <module>
    inc()
  File "tt.py", line 3, in inc
    x +=1
UnboundLocalError: local variable 'x' referenced before assignment
palash@ash:~/PycharmProjects/src$

my python version : Python 3.6.1 :: Anaconda custom (32-bit)

and I'm running on Ubuntu 17.04 32bit

I can't understand what is happening. Help me please

Edit: And can anybody define what is UnboundLocalError?

And Why "__main__"""is working ?

polu_
  • 342
  • 7
  • 17
  • Add `global x` in `inc`. Although it is better to wrap `x` in an object and have an inc method, rather than mutating global state. – Paul Rooney Jul 14 '17 at 04:42
  • Actually, I would much rather you passed in and returned x as a function parameter. Also, there's something off about your quotation marks. – cs95 Jul 14 '17 at 04:44
  • @coldspeed sorry for extra quotation marks. I removed that. That was a mistake. – polu_ Jul 14 '17 at 05:28
  • 1
    technically valid syntax though, implicit join of two literals `"__main__"` and `""`, looks like a heavily weighted triple-quoted string though :) – anthony sottile Jul 14 '17 at 05:33
  • Thank you for explaining – polu_ Jul 14 '17 at 05:35

1 Answers1

0

Try this:

x = 1


def inc():
    global x
    x += 1
    print(x)


if __name__ == "__main__""":
    inc()

Here x is a 'global variable' and if we want to use this variable inside a function, we must declare it as global before hand. Otherwise, Python would consider the variable as a local variable but there is no prior initialization of that before the statement that tries to increment it. That's why it was not working. And we should not use global variables. Rather we could pass that variable as an argument to the function as follows:

def inc(x):
    x += 1
    print(x)

Now, x is a local variable and we can call that function by passing an argument that will be used as the value of the local variable. To call the function, we use this statement: inc(1).

For more, go through these posts:

arif
  • 524
  • 8
  • 18
  • 1
    Thanks. It's working now. – polu_ Jul 14 '17 at 05:33
  • 1
    Your second example is not the same as the original. This is because it is only incrementing the local value of ``x`` with local scope created when passed to the function, it is not changing the variable at global scope as it was before. So if the intent was that the global should be changed after the call, you can't use the latter. – Graham Dumpleton Jul 14 '17 at 05:44
  • For me the first example is working. – polu_ Jul 14 '17 at 05:50
  • If that global variable `x` is meant to be used by other functions as well, then yes the second version would not be applicable. The answer is based on the given scenario, a global variable, and a function. I should have clarified that in the answer. Thanks, @GrahamDumpleton. My intention was to emphasize that we should avoid global variables if we have the chance. – arif Jul 14 '17 at 05:50