2

When I run this code I don't get a error. I'm trying to calculate l1 and put into variable a.

m=np.linspace(-np.pi,np.pi,1000)
l1="np.sin(m)"
exec(f"a ={l1}")
print(a)

When I run this I get a error.Why?

def g():
    m=np.linspace(-np.pi,np.pi,1000)
    l1="np.sin(m)"
    exec(f"a ={l1}")
    print(a)
g()

Error:name "a" is not defined

1 Answer 1

2

Check link: Setting variables with exec inside a function

def g():
    m=np.linspace(-np.pi,np.pi,1000)
    l1="np.sin(m)"
    exec(f"global a; a={l1}")
    print(a)
g()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.