```python
```python """ 这个模块包含一些有用的函数。
Example: >>> add(2, 3) 5 """
def add(x, y): """ 将两个数相加。
Args: x: 第一个数。 y: 第二个数。
Returns: 两个数的和。
Raises: TypeError: 如果输入不是数字。 """ if not isinstance(x, (int, float)) or not isinstance(y, (int, float)): raise TypeError("输入必须是数字") return x + y
def subtract(x, y): """ 从第一个数中减去第二个数。
Args: x: 第一个数。 y: 第二个数。
Returns: 两个数的差。
Raises: TypeError: 如果输入不是数字。 """ if not isinstance(x, (int, float)) or not isinstance(y, (int, float)): raise TypeError("输入必须是数字") return x - y
def multiply(x, y): """ 将两个数相乘。
Args: x: 第一个数。 y: 第二个数。
Returns: 两个数的积。
Raises: TypeError: 如果输入不是数字。 """ if not isinstance(x, (int, float)) or not isinstance(y, (int, float)): raise TypeError("输入必须是数字") return x * y
def divide(x, y): """ 将第一个数除以第二个数。
Args: x: 第一个数。 y: 第二个数。
Returns: 两个数的商。
Raises: TypeError: 如果输入不是数字。 ZeroDivisionError: 如果第二个数是0。 """ if not isinstance(x, (int, float)) or not isinstance(y, (int, float)): raise TypeError("输入必须是数字") if y == 0: raise ZeroDivisionError("不能除以0") return x / y ```
- 本文标签: Python
- 本文链接: https://blog.sandy1029.cloud/article/256
- 版权声明: 本文由nisan原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权