I tried with exec()
but it doesn't work for what i'm trying to do:
math = "1 + 1"
a = exec(math)
print(a)
when "a" is printed i get
None
how can i do something like this?
I tried with exec()
but it doesn't work for what i'm trying to do:
math = "1 + 1"
a = exec(math)
print(a)
when "a" is printed i get
None
how can i do something like this?
exec("a = 1 + 1")
print(a)
2
b = eval("1 + 1")
print(b)
2
You're getting None
because exec
always returns None
, instead it execute the expression. What you might want is eval
, it returns whatever is evaluated inside.
Notice that both of them are quite dangerous as malicious programs can easily assess your data by input.