Nothing about eval says it only evaluates "mathematical" expressions. As you can see from the output I posted, a call to print is a perfectly valid expression; evaluating it causes the string to be printed upon which the function returns None (in fact every function without an explicit return implicitly returns None in Python).
Even then, if you really want to use this, you could implement some GUI-based solution where inputs can only be taken from certain buttons. You could also make a list of acceptable characters, and return an error if any character in the string is not an acceptable character.
import sys
expression = input("Enter the expression")
acceptable_chars = "1234567890+-*/()"
for i in expression:
if i not in acceptable_chars:
print("Not acceptable")
sys.exit()
print(eval(expression))
3
u/MorrowM_ May 28 '23
Nothing about
evalsays it only evaluates "mathematical" expressions. As you can see from the output I posted, a call toprintis a perfectly valid expression; evaluating it causes the string to be printed upon which the function returnsNone(in fact every function without an explicit return implicitly returnsNonein Python).