Hello I am newbie to Python. While learning I came across below code snippets in that I am unable to understand behaviour of the code. Here is first case
#Case 1
x=1
def func():
x=5
print(x)
func()
5
Here is second case
#Case 2
x=1
def func():
print(x) #First print statement
x=5
print(x) #Second print statement
func()
UnboundLocalError: local variable 'x' referenced before assignment
The two cases are same except the second has one additional print statement. Why the first print statement of second snippet making the Python to throw an exception?