def beat():
exec('nDns=1', locals())
print(nDns)
beat()
Result NameError: name 'nDns' is not defined. When I change locals() to globals() it works, but I don't really want it written to globals. It must stay in scope of the function. How?