计算器加减乘除python代码简单
时间: 2023-03-10 21:31:51 浏览: 275
我可以为你提供一个简单的Python代码,用来计算加减乘除:# 计算加减乘除num1 = int(input('输入第一个数字:'))
num2 = int(input('输入第二个数字:'))print('1.加法')
print('2.减法')
print('3.乘法')
print('4.除法')choice = input('输入你的选择:')if choice == '1':
print(num1, '+', num2, '=', num1+num2)
elif choice == '2':
print(num1, '-', num2, '=', num1-num2)
elif choice == '3':
print(num1, '*', num2, '=', num1*num2)
elif choice == '4':
print(num1, '/', num2, '=', num1/num2)
else:
print('错误的输入!')
相关问题
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:
raise ValueError("除数不能为零!")
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':
try:
print(num1, "/", num2, "=", divide(num1, num2))
except ValueError as e:
print(e)
else:
print("非法输入")
```
你可以根据自己的需要进行扩展和修改。这个简单的计算器能够执行基本的加减乘除运算,并且对除数为零的情况进行了处理。
用python实现简单计算器加减乘除
可以使用Python的数学库或者内置函数来实现简单的计算器,例如:
```
num1 = float(input("请输入一个数字: "))
num2 = float(input("请输入另一个数字: "))
operation = input("请选择操作 (+, -, *, /): ")
if operation == '+':
result = num1 + num2
elif operation == '-':
result = num1 - num2
elif operation == '*':
result = num1 * num2
elif operation == '/':
result = num1 / num2
else:
print("无效的操作符")
print("结果为: ", result)
```
注意,这个计算器只能处理简单的加减乘除运算,对于复杂的数学问题需要使用更加专业的工具和算法。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)