本题目要求读入一个由任意整数组成的列表,输出所有元素中的最大值、最小值和平均值,要求平均值保留2位小数。用python实现
时间: 2024-06-17 20:05:46 浏览: 12
好的,可以使用以下代码实现:
```python
num_list = list(map(int, input().split())) # 读入整数列表
max_num = max(num_list) # 最大值
min_num = min(num_list) # 最小值
avg_num = round(sum(num_list)/len(num_list), 2) # 平均值,保留两位小数
print("最大值为:", max_num)
print("最小值为:", min_num)
print("平均值为:", avg_num)
```
相关问题
本题目要求读入一个由任意整数组成的列表,输出所有元素中的最大值、最小值和平均值,要求平均值保留2位小数
好的,这是一个简单的统计问题。我们可以先读入列表,然后找到其中的最大值、最小值和总和,最后计算平均值即可。以下是代码示例:
```python
nums = input().split()
nums = [int(num) for num in nums] # 将输入的字符串转换为整数列表
max_num = max(nums)
min_num = min(nums)
sum_num = sum(nums)
average = sum_num / len(nums)
print("最大值:", max_num)
print("最小值:", min_num)
print("平均值:", format(average, '.2f'))
```
输入格式示例:
```
1 2 3 4 5
```
输出结果示例:
```
最大值: 5
最小值: 1
平均值: 3.00
```
用Python要求读入一个由任意整数组成的列表,输出所有元素中的最大值、最小值和平均值,要求平均值保留2位小数。
好的,这个问题很简单,下面是Python代码实现:
```python
lst = list(map(int, input().split())) # 读入整数列表
max_val = max(lst) # 获取最大值
min_val = min(lst) # 获取最小值
avg_val = sum(lst) / len(lst) # 获取平均值
print("最大值:", max_val)
print("最小值:", min_val)
print("平均值:", round(avg_val, 2))
```
你可以将任意整数列表输入到程序中,程序会输出该列表的最大值、最小值和平均值。注意,平均值保留2位小数,使用`round()`函数实现。
相关推荐
![](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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)