0

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'
2

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm. But if someone could see eval() it could turn out to be really dangerous
@Sujay I mentioned that under
its insecure, because user can enter other thing and reveal my code, (is it correct?) so what if I use if statement to limit user and let him just use some special characters, like 1-9 and * and / and + and - ....?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.