你如何在python中制作计算器?

问题描述 投票:-4回答:1

我试图让提示允许用户输入等式。我希望它输出答案。我知道我的代码错了,所以你们可以提供帮助,这样我就能从提示中输出方程式输出吗?谢谢。

python python-2.7
1个回答
0
投票

这是一个简单的开始。

from __future__ import division # 4/3 gives 1.0 without this
import math
# operators you can use:
#    "(", ")", "*", "/", "+", "-"
#    "math.sin()", "math.cos()", "math.tan()", "math.pi"
#    "**"
while True:
    result = eval(raw_input("Enter equation: ")) 
    result = float(result)
    print "result = " + str(result)
#
# (4+2)/3 = 2.0
# 3*7 = 21.0
# 2+(3-1) = 4.0
# math.sin(0) = 0.0
# math.cos(0) = 1.0
# math.asin(0) = 0.0
# math.acos(0) = 1.57079632679
# math.pi = 3.14159265359
# 2**2 = 4.0
# 9**0.5 = 3.0
© www.soinside.com 2019 - 2024. All rights reserved.