2
x = """
def test_add(a, b):
    add = a + b
    return add

test_add(10, 6)
"""
print(eval(x))

The above code produces following error:

File "<string>", line 2
    def test_add(a, b):
      ^
SyntaxError: invalid syntax

How can i fix this error ? Note : In this example, 16 should be printed to the console.

2
  • I believe eval only evaluates expressions. Commented Mar 21, 2021 at 13:15
  • Eval expects an expression an returns its value. You need exec and put the print statement inside the string because exec return None. Commented Mar 21, 2021 at 13:16

1 Answer 1

-2

The eval() method can returns the result evaluated from the expression.

a = 10
b = 6
print(eval("a+b"))
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, and? How does that answer the question? You didn't explain why the code in the question doesn't work or how to fix it...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.