Problem statement: Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number.
示例输出(忽略项目符号,不知道如何将文本格式化为控制台输出):输入一个整数,如果是0:1,则输入结束
输入一个整数,如果是0:2,则输入结束
输入一个整数,如果为0,则输入结束:-1
输入一个整数,如果是0:3,则输入结束
输入一个整数,如果是0:0,则输入结束
你没有输入任何号码
阳性数是3
负片的数目是1
总数是5
平均值是1.25
尝试的解决方案:def main():
i = int( input ("Enter an interger, the input ends if it is 0: "))
count_pos = 0
count_neg = 0
total = 0
if (i != 0):
while (i != 0):
if (i > 0):
count_pos += 1
elif (i < 0):
count_neg += 1
total += i
i = int( input ("Enter an interger, the input ends if it is 0: "))
count = count_pos + count_neg
average = total / count
print ("The number of positives is", count_pos)
print ("The number of negatives is", count_neg)
print ("The total is", total)
print ("The average is", float(average))
else:
print ("You didn't enter any number.")
main()