用python编写一个计算器
时间: 2023-09-18 07:14:41 浏览: 103
好的,以下是 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 "不能除以 0"
else:
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':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
else:
print("您输入的操作不正确!")
```
请问还有什么问题吗?
阅读全文