1

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
0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.