Dec-27-2019, 05:23 AM
Hi
Need some help here
Original code
I used both global , nonlocal but both throws different error
nonlocal test_var ^
SyntaxError: invalid syntax
NameError: global name 'test_var' is not defined
Any suggestions ? I have gone through top 10 search results , all suggest to use nonlocal or global...
Need some help here
Original code
def test1():
test_list = []
test_var = 0
test_string1 = ""
def test2():
temp =2
if temp > test_var:
test_var = temp
else:
test_string = "fail"
return test_string
res = test2()
print res
test1()https://docs.python.org/3/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-valueI used both global , nonlocal but both throws different error
def test1():
test_list = []
test_var = 0
test_string1 = ""
def test2():
nonlocal test_var
temp =2
if temp > test_var:
test_var = temp
else:
test_string = "fail"
return test_string
res = test2()
print res
test1()throws this error nonlocal test_var ^
SyntaxError: invalid syntax
def test1():
test_list = []
test_var = 0
test_string1 = ""
def test2():
global test_var
temp =2
if temp > test_var:
test_var = temp
else:
test_string = "fail"
return test_string
res = test2()
print res
test1()throws this error ^NameError: global name 'test_var' is not defined
Any suggestions ? I have gone through top 10 search results , all suggest to use nonlocal or global...
