Entering edit mode
5.7 years ago
mdsiddra
▴
30
I am working with certain codes in python and I wanted to know if a certain value of a variable can be accesssed outside the function without explicitly declaring it a global variable. for example:
I was using this:
def func1():
v1 = "value1"
return v1
print ("This is the first function")
def func2():
v2 = "value2"
func1()
print ("This is the second function")
fo = input ("Enter 1/2\n")
if fo == '1':
func1()
elif fo == '2':
func2()
else:
print ("invalid")
print (v1)
It results me this error:
print (v1)
NameError: name 'v1' is not defined
Can I use this value of 'v1'
variable outside the function without defining it to b 'global'
??