(统计正数和负数的个数然后计算这些数的平均值)编写一个程序来读人不指定个数的整数,然后决定已经读取的整数中有多少个正数和多少个负数并计算这些输入值(不统计0)的总和,最得出它们的平均值。这个程序以输入值 0
时间: 2023-11-30 22:21:30 浏览: 50
作为结束标志。提示:使用一个变量来计算正数的个数,另一个变量来计算负数的个数,第三个变量来计算输入的数的总和,最后再计算平均值。
示例输出:
Enter an integer, the input ends if it is 0: 1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5.0
The average is 1.25
Enter an integer, the input ends if it is 0: 0
No numbers are entered except 0
Enter an integer, the input ends if it is 0: -1 -2 -3 0
The number of positives is 0
The number of negatives is 3
The total is -6.0
The average is -2.0
提示: 可以使用while循环来读入数据,如果读入0,则退出循环。在循环中可以使用if语句来判断输入的数是正数还是负数,并统计它们的个数。在循环结束后,根据正数和负数的个数以及输入的数的总和计算平均值。注意,需要判断输入的数是否为0,因为题目要求不统计0。
相关问题
python统计正数和负数的个数,编写一个程序来读入不指定个数的整数,然后统计读取的整数中有多少个正数和多少负数并计算这些输入值的平均值
好的,下面是 Python 的代码实现:
```python
positive_count = 0
negative_count = 0
positive_sum = 0
negative_sum = 0
input_count = 0
while True:
try:
num = int(input("请输入一个整数:"))
except:
break
input_count += 1
if num > 0:
positive_count += 1
positive_sum += num
elif num < 0:
negative_count += 1
negative_sum += num
if input_count == 0:
print("没有输入整数")
else:
positive_avg = positive_sum / positive_count if positive_count > 0 else 0
negative_avg = negative_sum / negative_count if negative_count > 0 else 0
print("正数个数为:", positive_count)
print("负数个数为:", negative_count)
print("正数平均值为:", positive_avg)
print("负数平均值为:", negative_avg)
```
这段代码使用了一个无限循环,每次读取一个整数,并根据正负数分别累加个数和总值。如果输入不是整数,则跳出循环。最后根据累加的个数和总值计算平均值,并输出结果。
统计正数和负数的个数并求总和平均值py
以下是统计正数和负数的个数并求总和平均值的 Python 代码:
```python
nums = [] # 定义一个空列表用于存储数值
# 循环输入数值,直到输入的数值为 0
while True:
num = int(input("请输入一个整数(输入 0 结束输入):"))
if num == 0:
break
nums.append(num)
# 统计正数和负数的个数并求总和
positive_count = 0 # 正数个数
negative_count = 0 # 负数个数
total_sum = 0 # 总和
for num in nums:
if num > 0:
positive_count += 1
elif num < 0:
negative_count += 1
total_sum += num
# 计算平均值
if len(nums) > 0:
average = total_sum / len(nums)
else:
average = 0
# 输出结果
print("正数个数:", positive_count)
print("负数个数:", negative_count)
print("总和:", total_sum)
print("平均值:", average)
```
运行代码后,输入一些整数,最后会输出统计结果,例如:
```
请输入一个整数(输入 0 结束输入):5
请输入一个整数(输入 0 结束输入):-3
请输入一个整数(输入 0 结束输入):7
请输入一个整数(输入 0 结束输入):-2
请输入一个整数(输入 0 结束输入):0
正数个数: 2
负数个数: 2
总和: 7
平均值: 1.75
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)