从键盘输入非0整数,以输入0为输入结束标志,求平均值,统计正数负数个数,python
时间: 2024-05-22 10:12:36 浏览: 117
统计你输入的正负数的个数
num = int(input("请输入一个非0整数:"))
pos_count = 0 # 正数个数
neg_count = 0 # 负数个数
sum = num
count = 1
while num != 0:
num = int(input("请输入一个非0整数:"))
if num > 0:
pos_count += 1
elif num < 0:
neg_count += 1
sum += num
count += 1
if count == 1:
print("没有输入数字")
else:
avg = sum / (count - 1)
print("平均值为:", avg)
print("正数个数为:", pos_count)
print("负数个数为:", neg_count)
阅读全文