Python计算器程序【加减乘除操作】
**
方法一:
**
a = float(input("请输入第一个数:"))
b = input("请输入运算符:")
c = float(input("请输入第二个数:"))
d = a + c
e = a - c
f = a * c
g = a / c
h = "+"
i = "-"
j = "*"
k = "/"
if h == b:
print("结果是%s"%d)
elif i == b:
print("结果是%s"%e)
elif j == b:
print("结果是%s"%f)
elif k == b:
print("结果是%s"%g)
else:
print("输入错误,请重新输入")
举例输出:
**
方法二:
**
num1,num2 = eval(input("请输入第一个和第二个数(数与数之间使用逗号相隔):"))
op = input("请输入您要运算的符号:")
if op == "+":
print("{} + {} = {}".format(num1,num2,(num1 + num2)))
elif op == "-":
print("{} - {} = {}".format(num1,num2,(num1 - num2)))
elif op == "*":
print("{} * {} = {}".format(num1,num2,(num1 * num2)))
elif op == "/":
print("{} / {} = {}".format(num1,num2,(num1 / num2)))
elif op == "**":
print("{} ** {} = {}".format(num1,num2,(num1 ** num2)))