Python小练习(2)—计算器
请输入x 请输入y 请输入(+ - * / **) 输出正确结果
方法1
x = float(input("请输入一个数"))
y = float(input("请输入一个数"))
operator = str(input("请输入(+ - * / **)"))
if operator == "+":
z = x + y
print(z)
elif operator == "-":
z = x - y
print(z)
elif operator == "*":
z = x * y
print(z)
elif operator == "/":
if y ==0:
print("除数不能为0")
else:
z = x / y
print(z)
elif operator == "**":
z = x ** y
print(z)
else:
print("输入有误")
方法2
x = int(input("请输入数字x"))
y = int(input("请输入数字y"))
z = input("请输入运算符 + - * / **")
if z == "+":
print("%s+%s=%s" % (x, y, x + y))
elif z == "-":
print("%s-%s=%s" % (x, y, x - y))
elif z == "*":
print("%s*%s=%s" % (x, y, x * y))
elif z == "/":
if y != 0:
print("%s/%s=%s" % (x, y, x / y))
else:
print("除数不能为0")
elif z == "**":
print("%s**%s=%s" % (x, y, x ** y))
else:
print("小弟不才,没开发出其他运算符")