Off topic:accessing variable value outside the function
2
1
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'??

python • 627 views
ADD COMMENT
This thread is not open. No new answers may be added
Traffic: 1472 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6