Python- Labeling items in a list by year -
basically writing program reads text file , places each line file list. file lists population, in thousands, of united states during years 1950 - 1990. file, however, not list year, need program label each population number year.
my assignment is:
"you write program (name analysis_population.py) read contents of file above list, , calculate , display:
the average annual change in population during time period 1950 - 1990 (inclusive)
the year greatest increase in population during time period 1950 - 1990 (inclusive)
the year the smallest increase in population during time period 1950 - 1990 (inclusive)"
i have working except year label.
my code far follows:
year = 1950 open("uspopulation.txt", "r") f: poplist = [line.strip() line in f] poplist = [ int(x) x in poplist ] x in poplist: year = year + 1 diff = [abs(j-i) i,j in zip(poplist, poplist[1:])] maxdiff = max(abs(x - y) (x, y) in zip(poplist[1:], poplist[:-1])) mindiff = min(abs(x - y) (x, y) in zip(poplist[1:], poplist[:-1])) print("the average annual change in population during time period 1950 - 1990 is:\n", (sum(diff)/len(diff))) print("\nthe year greatest increase in population during time period 1950 - 1990 is:", year, "-", year + 1, "with increase of", maxdiff, "people.") mindiff = min(abs(x - y) (x, y) in zip(poplist[1:], poplist[:-1])) print("\nthe year smallest increase in population during time period 1950 - 1990 is:", year, "-", year + 1, "with increase of", mindiff, "people.")
the output is:
the average annual change in population during time period 1950 - 1990 is: 2443.875 year greatest increase in population during time period 1950 - 1990 is: 1991 - 1992 increase of 3185 people. year smallest increase in population during time period 1950 - 1990 is: 1991 - 1992 increase of 1881 people.
as can see, outputs same, incorrect year each time. correcting appreciated.
Comments
Post a Comment