In: Computer Science
Starting out with python
Lists and Tuples - US Population Data
In this assignment, you will be reading the contents of a file into a list.
This file contains the midyear population of the United States, in thousands, during the years 1950 through 1990. The first line in the file contains the population for 1950, the second line contains the populations for 1951, and so forth.
You will ask the user to input a specific year to check the population on.
Your program will then read the file's contents into a list. The program should display the following data:
1. The population for the year the user-specified.
2. The average annual change in population during the time period.
3. The year with the greatest increase in population during the time period.
4. The year with the smallest increase in population during the time period.
US Population:
151868 153982 156393 158956 161884 165069 168088 171187 174149 177135 179979 182992 185771 188483 191141 193526 195576 197457 199399 201385 203984 206827 209284 211357 213342 215465 217563 219760 222095 224567 227225 229466 231664 233792 235825 237924 240133 242289 244499 246819 249623
path = "USPopulation.txt"
with open(path) as fp:
population_list = list(map(int, fp.read().split()))
year = list(range(1950, 1991))
population_year = int(input("Input the Year: "))
if (population_year >= 1950 and population_year <= 1990):
idx = year.index(population_year)
print(f"The population for the year {population_year}.: ",population_list[idx])
else:
print("INVALID")
change = []
for i in range(0,len(population_list)-1):
change.append(population_list[i+1]-population_list[i])
total_elements_change = len(change)
print("The average annual change in population during the time period."+str(sum(change)/total_elements_change))
greatest_increase_pop = max(change)
greatest_increase_year = year[change.index(max(change))]
print("The year with the greatest increase in population during the time period."+str(greatest_increase_year))
smallest_increase_pop = min(change)
smallest_increase_year = year[change.index(min(change))]
print("The year with the smallest increase in population during the time period"+str(smallest_increase_year))
******************************************************************************************
PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT
SECTION
******************************************************************************************