无法保持某些数字的平均值

时间:2014-02-26 03:35:50

标签: python-3.x

我需要帮助。我正在尝试运行下面的程序。当我输入一个大于100或低于0的数字时,我需要它忽略该输入的任何建议吗?

total = 0.0
count = 0
data = int(input("Enter a number or 999 to quit: "))
while data != "":
    count += 1
    number = float(data)
    total += number
    data = int(input("Enter a number or 999 to quit: "))
    try:
        data = int(data)
    except ValueError:
        pass
    average = round(total) / count
    if data == 999:
        break
    elif data >= 100:
        print("error in value")
    elif data <= 0:
        number = 0
        print("error in value")   


print("These", count, "scores average as: ", round(average, 1))

1 个答案:

答案 0 :(得分:0)

您可以在检查无效数字后移动average = ...行,并在检查无效数字时添加continue。所以最后一行最终会像这样结束:

    if data == 999:
        break
    elif data >= 100:
        print("error in value")
        continue
    elif data <= 0:
        print("error in value")
        continue
    average = round(total) / count