Answer the question
In order to leave comments, you need to log in
How to print the value of a variable from a function in python?
I need to output the value of the variable a from the fun() function.
How to do it?
Here is my code:
def fun():
a = int ( input () )
fun ()
if a == 3:
print ( '123' )
else:
print ( '456')
Traceback (most recent call last):
File "123.py", line 5, in <module>
if a == 3:
NameError: name 'a' is not defined
Answer the question
In order to leave comments, you need to log in
In this form, no way. Read about scope here , or here . It's better in a book.
In your case, as a solution:
def fun():
return int(input())
a = fun()
if a == 3:
print('123')
else:
print('456')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question