0

I write below code in python(version 3.6.2):

def test():
    temp = 'c=1'
    exec(temp)
    print(c)

test()

when run it, I get below error:

NameError: name 'c' is not defined

but when run below code :

temp='a=1'
exec(temp)
print(a)

it print 1.

How do I fix this?

seyed_vahid
  • 49
  • 2
  • 8

1 Answers1

0

The exec() function can only be followed by a string or an object. What you asked it to do is not one of those. You should try just defining c in another way or stop using exec()