python输入一批整数,输出其中的最大值和最小值,输入数字0结束。
时间: 2023-05-01 14:07:09 浏览: 83
可以使用以下代码实现:
numbers = input('请输入一批整数,以空格分隔:').split()
numbers = [int(num) for num in numbers]
max_num = max(numbers)
min_num = min(numbers)
sum_num = sum(numbers)
print('最大值为:', max_num)
print('最小值为:', min_num)
print('数列总和为:', sum_num)
相关问题
python 输入一批整数 输出最大值和最小值 输入数字0结束
好的,我明白了。以下是实现该功能的代码:
```python
numbers = []
while True:
num = int(input()) # 输入整数
if num == 0: # 输入数字0结束
break
numbers.append(num)
max_num = max(numbers) # 获取最大值
min_num = min(numbers) # 获取最小值
print("最大值为:", max_num)
print("最小值为:", min_num)
```
通过该代码,您可以输入一批整数,程序会输出这些数中的最大值和最小值,当您输入数字0时程序会结束。希望我的回答对您有所帮助!
python输入一批整数输出最大值和最小值 输入0结束
好的,问题收到。以下是回答:
您好!关于您的问题,可以使用以下 Python 代码实现:
```
n = int(input("请输入一批整数:"))
maxn = n
minn = n
while n != 0:
n = int(input())
if n > maxn:
maxn = n
if n < minn and n != 0:
minn = n
print("最大值为:", maxn)
print("最小值为:", minn)
```
其中,通过一个 while 循环来获取输入的整数,并在输入 0 时退出循环。同时通过一个 if 语句来更新最大值和最小值。
值得注意的是,这里默认输入的一批整数中至少有一个非 0 正整数,否则其最小值默认为 0。
相关推荐
![](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)