N
N
nonamefromzp2020-04-25 22:04:35
Python
nonamefromzp, 2020-04-25 22:04:35

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')


This is the error it gives:
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

1 answer(s)
I
Ivan Yakushenko, 2020-04-25
@nonamefromzp

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 question

Ask a Question

731 491 924 answers to any question