编写一个加减乘除运算的小系统,当输入为 0 时退出,用函数实现各项功能。 ***************************** * 1---------加法 * * 2---------减法 * * 3---------乘法 * * 4---------除法 * * 0---------退出 * ***************************** 请输入你的选择(0-4):
时间: 2023-07-06 13:39:52 浏览: 84
好的,您可以这样编写一个加减乘除运算的小系统:
```python
def add(a, b):
"""加法"""
return a + b
def sub(a, b):
"""减法"""
return a - b
def mul(a, b):
"""乘法"""
return a * b
def div(a, b):
"""除法"""
if b == 0:
print("除数不能为0!")
return None
else:
return a / b
while True:
print("*****************************")
print("* 1---------加法 *")
print("* 2---------减法 *")
print("* 3---------乘法 *")
print("* 4---------除法 *")
print("* 0---------退出 *")
print("*****************************")
choice = int(input("请输入你的选择(0-4):"))
if choice == 0:
print("退出运算系统!")
break
elif choice == 1:
a = float(input("请输入第一个加数:"))
b = float(input("请输入第二个加数:"))
print(f"{a} + {b} = {add(a, b)}")
elif choice == 2:
a = float(input("请输入被减数:"))
b = float(input("请输入减数:"))
print(f"{a} - {b} = {sub(a, b)}")
elif choice == 3:
a = float(input("请输入第一个乘数:"))
b = float(input("请输入第二个乘数:"))
print(f"{a} * {b} = {mul(a, b)}")
elif choice == 4:
a = float(input("请输入被除数:"))
b = float(input("请输入除数:"))
result = div(a, b)
if result is not None:
print(f"{a} / {b} = {result}")
else:
print("输入错误,请重新输入!")
```
程序中定义了加法、减法、乘法和除法的函数,当用户输入选择后,根据选择调用相应的函数进行计算。当用户输入 0 时,退出运算系统。程序中使用了循环,可以多次进行运算。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)