p ython简易计算器
时间: 2024-09-26 20:05:40 浏览: 59
51单片机实现简易计算器功能
Python简易计算器是一个基本的程序示例,它允许用户输入两个数字并执行加、减、乘、除等基本算术运算。以下是一个简单的命令行交互式的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 x / y
else:
print("错误:除数不能为零")
return None
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':
result = divide(num1, num2)
if result is not None:
print(num1, "/", num2, "=", result)
else:
print("无效的选择")
阅读全文