In python 3 I was wondering if there was a way to get a variable from an input like this:
x = input("-> ")
And if x was this:
x = "1 + 10"
Is there a way to perform this operation and return it as a different variable?
Devesh Kumar Singh
20.5k5 gold badges25 silver badges43 bronze badges
asked May 23, 2019 at 8:57
user9868018
1 Answer 1
You can use eval to evaluate the mathematical expression inside the variable like so.
In [1]: x = "1 + 10"
In [2]: eval(x)
Out[2]: 11
answered May 23, 2019 at 9:00
Devesh Kumar Singh
20.5k5 gold badges25 silver badges43 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py