1

So i have created a user input where you can type in a function and it calculates it into a graph but my problem is the input is a str and if i type 2*x it cant convert to integer because of the non interger letters

x = 1

#The input from the user
function = "2*x+1"

#Trying to print a calculated version of my function 
print(int(function))

this is just a prototype i want it to print the result like in this case "3" and not "2*x+1" if anyone knows the anwser pls write it thx in advance!

Jones
  • 15
  • 3
  • You can use [eval()](https://realpython.com/python-eval-function/) but [probably should not](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice). Better options [here](https://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string). – jarmod Nov 14 '21 at 17:15

0 Answers0