How to get a Computational string like ((2*3)+(3-1))+(4/2)) and return the answer in python?
I can't use eval func. the inputed pharase should include parantheses. The program must take into account priorities when calculating.
How to get a Computational string like ((2*3)+(3-1))+(4/2)) and return the answer in python?
I can't use eval func. the inputed pharase should include parantheses. The program must take into account priorities when calculating.
Use eval. like this: ans = eval('((2*3)+(3-1))+(4/2))')
for that you gotta use the eval() function. It makes a string into a python code. One way to apply it would be
def convert_to_code(some_string):
return eval(some_string)
user_input = input()
result = convert_to_code(user_input)
print(result)
I find it easier to have a dedicated funtion to return eval() in cases with mathematical equations that you'll have to change the value of a variable x multiple times. For example, if you know numerical methods, when trying to create a programm that calculates a polynomial's root, how i did it would be very usefull. But you could just use eval() it self.
Get the string with e = input("Your question :").
You can calcul the string with e = eval(e) and you can show it with print(e).
The complete code :
e = input("Your question :")
e = eval(e)
print(e)