Python 3计算器,如何解决eval

时间:2018-07-27 04:45:06

标签: python eval calculator

我正在研究Python编程:计算机科学概论(第二版)。在第二章的最后,练习之一是设计一个计算器。在此之前,他一直使用eval()进行计算。我知道使用eval不利于安全,但我不知道如何解决它。我现在总是可以使用它,以后再担心它,但是我想养成良好的习惯。

我希望用户将所需的计算结果作为一行输入,如下所示:

>>> input('Input your expression: ')
Input your expression: 4 * 5 / 2

我已经看过这篇文章Pros and Cons on designing a calculator with eval。答案给了他一个链接,该链接显示了如何解决它。他做事的方式似乎很复杂,我不知道他是怎么做的。

我认为我需要eval()。我的计算器代码如下。谢谢您的帮助!

# Calculator.py
# This program will allow the user to input a mathematical expression (using python syntax)
# it will then evaluate the expression and print the result. It will unfortunately use eval
# but I dont know how to get around this problem. The program will loop to allow for
# multiple calculations.

expression = 'blank'

print('Calculator')
print(' ')
print('Welcome! Please input your desired expression, using python syntax.')
print('When you are finished, input end, and the program will finish.')

# Until the user inputs end, the calculator will continue to repeat.
while expression != 'end':
    print('')
    expression= input('Input your expression: ')
# Checks if the last digit of expression is a digit. I think this is fairly foolproof.
# I used last digit instead of first, bc a negative sign could have been in the first digit.
    if expression[len(expression)-1].isdigit():
        print('= ', eval(expression))
# A way to distinguish between 'end' and other strings. After this prints and loops back,
# program should end.
    elif expression == 'end':
        print('Shutting down... ')
# If the user inputs something where the last digit is not a digit, and is
# not end, the program will notify them and repeat.
    else:
        print('Enter with only digits and operators!')

1 个答案:

答案 0 :(得分:0)

eval可能就是您想要的。不建议使用eval,因为它允许您应用的任何用户执行任意代码,这显然会导致安全漏洞。但是,由于您这样做是为了获得学习经验,而不是在公开发布的应用中进行,因此,这并不是您真正关心的问题。尤其是如果您只是在学习,我现在会暂时忽略此 (绝对不要在生产应用中执行此操作)。

您将要做:eval(input('please enter a expression')),它使您可以执行任意表达式。

在您链接的文章中,他们解释了eval还有两个可选参数,它们允许您限制eval可以执行哪些表达式。

他将第二个参数设置为{"__builtins__":None},以限制您使用任何全局函数(如果将其设置为{},则像abs这样的内置函数仍然可用)。

他将第三个参数设置为他希望允许用户执行的所有功能的字典,因为他只是限制了用户以前无法运行任何全局功能。