Python编程实现简易计算器
时间: 2023-11-19 13:49:07 浏览: 115
可以使用Python编程实现一个简易计算器。下面是一个示例代码:
```python
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Error: Division by zero"
return x / y
print("请选择操作:")
print("1. 相加")
print("2. 相减")
print("3. 相乘")
print("4. 相除")
choice = input("请选择操作(1/2/3/4): ")
num1 = float(input("请输入第一个数字: "))
num2 = float(input("请输入第二个数字: "))
if choice == '1':
result = add(num1, num2)
print(num1, "+", num2, "=", result)
elif choice == '2':
result = subtract(num1, num2)
print(num1, "-", num2, "=", result)
elif choice == '3':
result = multiply(num1, num2)
print(num1, "*", num2, "=", result)
elif choice == '4':
result = divide(num1, num2)
print(num1, "/", num2, "=", result)
else:
print("无效的输入")
```
阅读全文