7. 编写函数,可以接收任意多个整数并输出其中的最大值、最小值和所有整数之和
时间: 2024-05-03 19:16:43 浏览: 123
以下是可以实现上述功能的 Python 代码:
```python
def operate_integers(*args):
# 初始化最大值、最小值和总和
max_value = float('-inf')
min_value = float('inf')
total_sum = 0
# 遍历所有参数并更新最大值、最小值和总和
for num in args:
if num > max_value:
max_value = num
if num < min_value:
min_value = num
total_sum += num
# 输出最大值、最小值和总和
print("最大值:", max_value)
print("最小值:", min_value)
print("总和:", total_sum)
```
该函数使用了可变参数 `*args`,可以接收任意数量的整数作为参数。函数内部会遍历所有参数并更新最大值、最小值和总和。最后,函数会输出最大值、最小值和总和。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)