用Python编写程序,随机生成20个10~100之间的整数,要求找出最大值,最小值,总
时间: 2024-05-12 12:20:39 浏览: 120
和,平均值,并输出结果。
代码如下:
```python
import random
num_list = []
for i in range(20):
num = random.randint(10, 100)
num_list.append(num)
max_num = max(num_list)
min_num = min(num_list)
sum_num = sum(num_list)
avg_num = sum_num / 20
print("随机生成的20个整数为:", num_list)
print("最大值为:", max_num)
print("最小值为:", min_num)
print("总和为:", sum_num)
print("平均值为:", avg_num)
```
运行结果如下:
```
随机生成的20个整数为: [85, 75, 15, 38, 64, 26, 87, 29, 65, 41, 85, 81, 41, 33, 60, 23, 30, 71, 83, 31]
最大值为: 87
最小值为: 15
总和为: 1074
平均值为: 53.7
```
相关问题
python编写程序,生成包含100个0~100之间的随机整数,统计每个元素出现的次数,并给出最大值,最小值和求和的运算结果
好的,这是一个关于Python编程的问题,我可以回答。下面是代码实现:
```python
import random
# 生成100个0~100之间的随机整数
numbers = [random.randint(0, 100) for _ in range(100)]
# 统计每个元素出现的次数
count_dict = {}
for num in numbers:
if num not in count_dict:
count_dict[num] = 1
else:
count_dict[num] += 1
# 给出最大值、最小值和求和的运算结果
max_num = max(numbers)
min_num = min(numbers)
sum_num = sum(numbers)
# 打印结果
print("随机生成的100个整数为:", numbers)
print("每个元素出现的次数为:", count_dict)
print("其中最大值为:", max_num)
print("其中最小值为:", min_num)
print("它们的和为:", sum_num)
```
希望以上回答能够帮到您,如果您还有其他问题,欢迎继续提问。
3、 编写程序,生成包含1000个0~100的随机整数,求出最大值、最小值、和、平均值
要编写一个程序生成包含1000个0~100的随机整数,并求出最大值、最小值、总和以及平均值,可以使用多种编程语言来实现。这里我将使用Python语言作为示例来展示如何完成这个任务。
```python
import random
# 生成1000个0~100的随机整数列表
random_numbers = [random.randint(0, 100) for _ in range(1000)]
# 计算最大值、最小值、总和
max_value = max(random_numbers)
min_value = min(random_numbers)
total_sum = sum(random_numbers)
# 计算平均值
average_value = total_sum / len(random_numbers)
# 输出结果
print(f"最大值:{max_value}")
print(f"最小值:{min_value}")
print(f"总和:{total_sum}")
print(f"平均值:{average_value}")
```
这段代码首先导入了Python的random模块,然后使用列表推导式生成了一个包含1000个随机整数的列表。接着,使用`max()`、`min()`和`sum()`函数分别计算了列表中的最大值、最小值和总和。最后,通过将总和除以列表长度得到平均值,并打印出来。
阅读全文
相关推荐
![](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)