If I had a function that I made:
def a():
n = 2*2
How could I access n out of the function without calling a?
If I had a function that I made:
def a():
n = 2*2
How could I access n out of the function without calling a?
You cannot. You will need to define the variable outside of the function, or call the function and return it.
You need to return it, so do:
def a():
n = 2*2
return n
print(a())
Output:
4
You can also do print
, but return
is better, check this: What is the formal difference between "print" and "return"?