0

input:-

x = '''
def fun(x, y):
 print(x+y) 
a = int(input('enter the value of a: '))
b = int(input('enter the value of b: '))
fun(a, b)
'''
print(exec(x))

output:-

enter the value of a: 5
enter the value of b: 5
10
None
Matt Morgan
5,3334 gold badges24 silver badges34 bronze badges
asked Nov 14, 2020 at 15:53
3
  • 2
    I guess the ‘None’ comes from ‘exec()’ which you print. So just call the ‘exec()’ Commented Nov 14, 2020 at 15:55
  • 1
    If you want to get rid of the None you can just print the result inside x and not doing print(exec(x)) but only exec(x) and print what you want inside Commented Nov 14, 2020 at 16:46
  • 1
    Does this answer your question? Python3 exec, why returns None? Commented Nov 16, 2020 at 11:21

2 Answers 2

2

The None doesn't come from nowhere, that's the return value of the exec method and as you print it, so it shows up

Just do

x = '''
def fun(x, y):
 print(x+y) 
a = int(input('enter the value of a: '))
b = int(input('enter the value of b: '))
fun(a, b)
'''
exec(x)
answered Nov 14, 2020 at 15:55
Sign up to request clarification or add additional context in comments.

Comments

2

As @azro said, the None is the return value from the exec method.

However, if you want to retrieve the result from fun inside a variable res, you can do:

x = '''
def fun(x, y):
 return x + y
 
a = int(input('enter the value of a: '))
b = int(input('enter the value of b: '))
res = fun(a, b)
'''
exec(x)
print(res) # prints the sum of the 2 numbers you gave
answered Nov 14, 2020 at 16:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.