如何计算用户输入的整数均值可被数字整除?

时间:2017-11-18 05:18:16

标签: python python-3.x

到目前为止..我编码了这个:

While True:
    div_5 = []
    x = float(input("Please input an integer(negative if you want to stop: "))
    x = int(x)
    length = len(div_5)
    total_sum = sum(div_5)
    average = total_sum/length
    if x % 5:
        div_5.append(x)
    if x < 0:
        break
print(average)

当我运行代码时,它说第7行有一个ZeroDivisionError。我究竟做错了什么?我正在添加x,如果它可以被列表中的5整除,为什么这样说呢? 任何提示将有助于我是python的新手。

这就是我需要做的事情

enter image description here

我一直在这个工作一个小时,一次几个小时。我似乎无法正确理解。

3 个答案:

答案 0 :(得分:0)

nums = []
while True:
    x = int(input("Please input an integer(negative if you want to stop: "))

    if x < 0:
        div_5 = [x for x in nums if x % 5 == 0]
        try:
            print(sum(div_5)/float(len(div_5)))
        except ZeroDivisionError:
            print("No number divisible by 5")
        break

    nums.append(x)

事实上,你不需要保留不能被5整除的数字。这是一种忽略零的运行平均值的方法。

count = 0.
average = 0.
while True:
    x = int(input("Please input a positive integer(negative if you want to stop: "))

    if x < 0:
        print(average)
        break
    if x % 5 == 0 and x != 0:
        count += 1
        average = average * (count - 1)/count + (1./count) * x 

答案 1 :(得分:0)

你可以从这段代码中得到提示,但由于这似乎是学校的功课,所以我建议你如果你想改进你的编码就应该尝试自己。

user_input=[]
while True:

    x = int(input("Please input an integer(negative if you want to stop: "))
    if x>0:
        user_input.append(x)
    else:
        break

div_5=[item for item in user_input if item%5==0]
print('The mean is {}'.format(sum(div_5)/len(div_5)))

输出:

Please input an integer(negative if you want to stop: 1
Please input an integer(negative if you want to stop: 2
Please input an integer(negative if you want to stop: 3
Please input an integer(negative if you want to stop: 4
Please input an integer(negative if you want to stop: 5
Please input an integer(negative if you want to stop: 6
Please input an integer(negative if you want to stop: 7
Please input an integer(negative if you want to stop: 8
Please input an integer(negative if you want to stop: 9
Please input an integer(negative if you want to stop: 15
Please input an integer(negative if you want to stop: -12
The mean is 10.0

答案 2 :(得分:0)

正如我在评论中所说:

  1. 您需要在div_5循环开始之前创建while列表。
  2. 您应该读取整数输入,因此float调用无效。
  3. 在阅读后立即测试x是否为负数。
  4. 在你退出循环之前不要计算平均值,如果列表中没有数据,就不要计算它。
  5. 此外,您的可分性测试是向后的:如果x % 5可被5整除,则x为零,零被视为False。

    这是修复后的代码版本。

    div_5 = []
    while True:
        x = int(input("Please input an integer (negative if you want to stop): "))
        if x < 0:
            break
        if x % 5 == 0:
            div_5.append(x)
    
    if div_5:
        average = sum(div_5) / len(div_5)
    else:
        average = 0
    print(average)
    

    <强>演示

    Please input an integer (negative if you want to stop): 1
    Please input an integer (negative if you want to stop): 5
    Please input an integer (negative if you want to stop): 6
    Please input an integer (negative if you want to stop): 10
    Please input an integer (negative if you want to stop): -4
    7.5
    

    当我们突破主循环时,测试if div_5测试div_5列表中是否有任何数字,如果存在则仅执行除法。如果列表中没有数字,则平均值为零。