user2901098
user2901098

Reputation: 11

Average annual change?

I am working on a program that will pull the midyear population for the United States during the years 1950 through 1990. The first line pulls for 50, the next line has the population for 51, and so on. The program pulls the information, then displays the year with the greatest increase in population, the smallest increase in population, and the average annual change in population during the range of dates.

I can do the first two, but the average annual change is not displaying correctly. I was given a hint, and apparently I am missing a line at 30. The average annual change just keeps displaying as zero? :(

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

As requested, here are a few lines of the input file:

151868
153982
156393
158956
161884
165069
168088
171187
174149
177135
179979

This is just a basic .txt file, with the first line being the population of the States in 1950, the second being the population in 1951, and so on.

Upvotes: 0

Views: 4421

Answers (3)

Alex
Alex

Reputation: 1

If it's a big file you can use

for i in xrange(1, len(yearly_population)):
    change = yearly_population[i] - yearly_population[i-1]
    yearly_change.append(change) 

Upvotes: 0

iblazevic
iblazevic

Reputation: 2733

You need to add this line in your code to update yearly_change field:

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

This should store every change in field, so when you try to calculate average that list wont be empty anymore.

Upvotes: 0

Crowman
Crowman

Reputation: 25918

You do this:

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

but you set yearly_change to an empty list, here:

yearly_change = []

and then never change it. So you're trying to calculate the total of an empty list, and then trying to calculate the average of it, so it's obviously going to be zero. Your program should either be updating yearly_change in some way, or you should be calculating the sum of some other list.

Upvotes: 1

Related Questions