python数字计算公式_Python:(计算正数和负数并计算数字的平均值)

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()

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值