我是靠谱客的博主 阔达方盒,这篇文章主要介绍python求平均工资_Python :(计算正数和负数并计算数字的平均值),现在分享给大家,希望可以做个参考。

问题陈述:编写一个读取未指定数量的整数的程序,确定读取了多少正值和负值,并计算输入值的总和和平均值(不计算零)。程序以输入0结束。将平均值显示为浮点数。

示例输出(忽略项目符号,不知道如何将文本格式化为控制台输出):

输入一个整数,输入结束,如果它是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()

最后

以上就是阔达方盒最近收集整理的关于python求平均工资_Python :(计算正数和负数并计算数字的平均值)的全部内容,更多相关python求平均工资_Python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(144)

评论列表共有 0 条评论

立即
投稿
返回
顶部