0

我正在制定一个程序,该程序将在 1950 年到 1990 年期间为美国拉动年中人口。第一行拉到 50,下一行拉到 51 的人口,依此类推。程序提取信息,然后显示在日期范围内人口增长最大、人口增长最小以及人口年平均变化的年份。

我可以做前两个,但年平均变化显示不正确。有人给了我提示,显然我在 30 岁时漏掉了一条线。平均年变化一直显示为零?:(

def main():
    #setup variables
    yearly_change = []
    change=0.0
    total_change=0
    average_change=0
    greatest_increase=0
    smallest_increase=0
    greatest_year=0
    smallest_year=0
    BASE_YEAR=1950
    try:
        #open the file for reading
        input_file = open("USPopulation.txt", "r")
        #read all the lines in the in file into a list
        yearly_population= input_file.readlines()
        #turn all read lines into a number
        for i in range(len(yearly_population)):
            yearly_population[i] = float(yearly_population[i])
        #calculate the change in population size for each two years
        for i in range(1,len(yearly_population)):
            change = yearly_population[i] - yearly_population[i-1]

            #MISSING SINGLE LINE HERE? 

            #if this is the first year, set trackers to its value
            if i==1:
                greatest_increase = change
                smallest_increase = change
                greatest_year = 1
                smallest_year = 1
            #this is not the first change in population size
            #update the trackers if relevent
            else:
                if change>greatest_increase:
                    greatest_increase = change
                    greatest_year = i
                elif change<smallest_increase:
                    smallest_increase = change
                    smallest_year = i
            total_change = float(sum(yearly_change))
            average_change = total_change/40
            print("The average annual change in population during the time period is",\
                  format(average_change, '.2f'))
            print("The year with the greatest increase in population was",
BASE_YEAR+greatest_year)
            print("The year with the smallest increase in population was",
BASE_YEAR+smallest_year)
            input_file.close()
    except IOError:
        print("The file could not be found")
    except IndexError:
        print("There was an indexing error")
    except:
        print("An error occurred")

main()

根据要求,这里有几行输入文件:

151868
153982
156393
158956
161884
165069
168088
171187
174149
177135
179979

这只是一个基本的 .txt 文件,第一行是 1950 年的美国人口,第二行是 1951 年的人口,以此类推。

4

3 回答 3

1

你来做这件事:

total_change = float(sum(yearly_change))
average_change = total_change/40

但你设置yearly_change为一个空列表,这里:

yearly_change = []

然后永远不要改变它。所以你试图计算一个空列表的总数,然后试图计算它的平均值,所以它显然会为零。您的程序应该yearly_change以某种方式更新,或者您应该计算其他列表的总和。

于 2013-10-20T21:42:54.327 回答
0

如果它是一个大文件,你可以使用

for i in xrange(1, len(yearly_population)):
    change = yearly_population[i] - yearly_population[i-1]
    yearly_change.append(change) 
于 2013-10-20T21:57:51.327 回答
0

您需要在代码中添加此行以更新yearly_change字段:

    for i in range(1,len(yearly_population)):
        change = yearly_population[i] - yearly_population[i-1]
        yearly_change.append(change)  #new line

这应该存储字段中的每个更改,因此当您尝试计算平均值时,该列表将不再为空。

于 2013-10-20T21:49:13.633 回答