python : local variable is referenced before assignment
python
Solution
When you want to access a global variable, you can just access it by its name. But if you want to change its value, you need to use the keyword `global`.
try :
global x
x = x * y
return x
In case 2, x is created as a local variable, the global x is never used.
>>> x = 12
>>> def poi():
... x = 99
... return x
...
>>> poi()
99
>>> x
12
Problem
Here is my code : ``` x = 1 def poi(y): # insert line here def main(): print poi(1) if __name__ == "__main__": main() ``` If following 4 lines are placed, one at a time, in place of `# insert line here` ``` Lines | Output ---------------+-------------- 1. return x | 1 2. x = 99 | return x | 99 3. return x+y | 2 4. x = 99 | 99 ``` In above lines it seems that global x declared above function is being used in case 1 and 3 But , ``` x = x*y return x ``` This gives ``` error : local variable 'x' is reference before assignment ``` What is wrong in here ?