python - solve equations using multiple values of x -
i need on fallowing: lets ask user ecuation can anything, illustrating example lets the user choose one:
x**5+3, , needs assign value of x, this: write equation: write value of x want calculate:
my question is: how can modify x in equation user gave first assign value wants calculate?? in other worlds how can make python calculate x**5+2, x= input value??
it looks want user enter equation variables , value variable. , want code evaluate user input equation user input value variable in equation. sounds candidate eval()
:
in [185]: equation = 'x ** 5 + 2' in [186]: x = 3 in [187]: eval(equation) out[187]: 245
another example:
in [188]: equation = '(x + 99) * 2' in [189]: x = 1 in [190]: eval(equation) out[190]: 200
since in above demonstration, equation
string, might user input. when ask user input equation, store in variable (variable equation
here). when ask them value variables in equation, cast int
, eval(equation)
in code.
Comments
Post a Comment