计算while循环中raw_input数据的平均值

时间:2018-03-02 10:27:47

标签: python python-2.7

研究员python开发人员。我有一项任务,我似乎无法破解,我的导师是MIA。我需要编写一个只使用GUI.Search_ListBox.Clear GUI.Search_ListBox.ColumnCount = 5 ' Columns GUI.Search_ListBox.ColumnWidths = "120;80;70;120;300" GUI.Search_ListBox.ColumnHeads = True 'GUI.Search_ListBox.RowSource = "Hello;gkjfl;hsjgh;hdfjhgkj;fdjghjkdf" 'here it fails!!! 循环,whileifelif语句的程序:

  • 不断要求用户使用else输入任意随机正整数

  • 用户输入-1后,程序需要结束

  • 程序必须计算用户输入的数字的平均值(不包括-1)并打印出来。

这就是我到目前为止:

int(raw_input())

8 个答案:

答案 0 :(得分:1)

尝试以下内容并提出您可能遇到的任何问题

$ python manage.py cleanup
$ pyclean .
$ find . -name "*.pyc" -exec rm -f {} \;
$ find . -name \*.pyc -delete
$ python manage.py clean_pyc

答案 1 :(得分:0)

您需要在代码末尾添加计算平均值。

为此,请计算while循环运行的次数,并将结束时的答案除以该值。

此外,由于行CREATE VIEW dbo.V1 WITH SCHEMABINDING AS SELECT SUM(ISNULL(DATEDIFF(minute, ac.StartTime, ac.EndTime), 0)) AS Effort, SUM(ISNULL(DATEDIFF(minute, ut.StartTime, ut.EndTime), 0)) AS Availability, ... CREATE INDEX IX_V1 on dbo.V1 (...) CREATE VIEW dbo.V2 WITH SCHMEBINDING AS SELECT Effort, Availability, Effort/Availability as Utilisation /* Other calcs */ FROM dbo.V1 WITH (NOEXPAND) ,您的代码每次都会在答案中添加一个,这不会产生正确的平均值。并且您缺少存储第一个输入数字,因为代码连续输入两个输入。这是一个固定版本:

answer = counter + anyNumber

答案 2 :(得分:0)

我认为还有另一个问题:

anyNumber = int(raw_input("Enter any number: "))
while anyNumber > num:
        anyNumber = int(raw_input("Enter another number: "))

变量anyNumber的值在循环之前和循环开始时更新,这意味着你要输入的第一个值永远不会被考虑在平均值中。

答案 3 :(得分:0)

另一种解决方案,使用更多功能,但更安全。

import numpy as np

numbers = []

while True:
    # try and except to avoid errors when the input is not an integer.
    # Replace int by float if you want to take into account float numbers
    try:
        user_input = int(input("Enter any number: "))

        # Condition to get out of the while loop
        if user_input == -1:
            break

        numbers.append(user_input)
        print (np.mean(numbers))

    except:
        print ("Enter a number.")

答案 4 :(得分:0)

虽然@ nj2237提供的解决方案也很完美,但这是我自己想要做的事情。

# Initialization
sum = 0
counter = 0
stop = False

# Process loop
while (not stop):
    inputValue = int(raw_input("Enter a number: "))

    if (inputValue == -1):
        stop = True
    else:
        sum += inputValue
        counter += 1 # counter++ doesn't work in python

# Output calculation and display
if (counter != 0):
    mean = sum / counter
    print("Mean value is " + str(mean))

print("kthxbye")

答案 5 :(得分:0)

您不需要保存总数,因为如果数字真的很大,您可能会有溢出。仅使用平均值应该足够了:

STOP_NUMBER = -1
new_number = int(raw_input("Enter any number: "))
count = 1
avg = 0
while new_number != STOP_NUMBER:
    avg *= (count-1)/count
    avg += new_number/count
    new_number = int(raw_input("Enter any number: "))
    count += 1

答案 6 :(得分:0)

我建议关注。

counter = input_sum = input_value = 0

while input_value != -1:
    counter += 1
    input_sum += input_value
    input_value = int(raw_input("Enter any number: "))

try:
    print(input_sum/counter)
except ZeroDivisionError:
    print(0)

您可以避免使用两个raw_input并使用while循环内的所有内容。

答案 7 :(得分:0)

还有另一种做法..

nums = []
while True:
    num = int(raw_input("Enter a positive number or to quit enter -1: "))
    if num == -1:
        if len(nums) > 0:
            print "Avg of {} is {}".format(nums,sum(nums)/len(nums))  
        break
    elif num < -1:
        continue
    else:
        nums.append(num)
相关问题