User enters an input like this: 4*3**2 Is there anyway to perform this, as a python code (not string)? I mean something like this:
x = input('Enter a operation: ') # 4*3**2
print('Answer is:', x) # expect: 'Answer is: 36'
User enters an input like this: 4*3**2 Is there anyway to perform this, as a python code (not string)? I mean something like this:
x = input('Enter a operation: ') # 4*3**2
print('Answer is:', x) # expect: 'Answer is: 36'
You could use eval
:
x = input('Enter a operation: ')
print('Answer is:', eval(x))
But the problem is that eval
is unsafe and inefficient, see this for more information.