键盘上任意输入20个整数,统计有多少个正 整数,多少个负整数,并求出正整数的平均值,负整数的平均值python
时间: 2024-05-19 14:15:47 浏览: 126
num_list = input("请输入20个整数,用空格隔开:").split()
positive_count = 0 # 正整数个数
positive_sum = 0 # 正整数总和
negative_count = 0 # 负整数个数
negative_sum = 0 # 负整数总和
for num in num_list:
num = int(num)
if num > 0:
positive_count += 1
positive_sum += num
elif num < 0:
negative_count += 1
negative_sum += num
if positive_count != 0:
positive_average = positive_sum / positive_count
print("正整数个数为:", positive_count)
print("正整数平均值为:", positive_average)
else:
print("没有正整数!")
if negative_count != 0:
negative_average = negative_sum / negative_count
print("负整数个数为:", negative_count)
print("负整数平均值为:", negative_average)
else:
print("没有负整数!")
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.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)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)