Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Python 3 please put comments so I understand why you did what you did. A copy

ID: 3596589 • Letter: I

Question

In Python 3 please put comments so I understand why you did what you did. A copy of the file is below.

FIPS,NAME,POP1997
53,Washington,5604260
30,Montana,888723
23,Maine,1244828
38,North Dakota,644782
46,South Dakota,736549
56,Wyoming,484529
55,Wisconsin,5189399
16,Idaho,1210819
50,Vermont,591659
27,Minnesota,4690847
41,Oregon,3245429
33,New Hampshire,1171443
19,Iowa,2859263
25,Massachusetts,6106984
31,Nebraska,1660613
36,New York,18177296
42,Pennsylvania,12051902
09,Connecticut,3277113
44,Rhode Island,988370
34,New Jersey,8018326
18,Indiana,5874844
32,Nevada,1652983
49,Utah,2034167

Modify the state population script to create a population lookup tool. Create a script to load a dictionary from the text file where the state name is the key and the population is the value. Prompt the user to enter a state name and then print the population as the response.

•Handle the case where the user enters an invalid state name

Explanation / Answer

# dictionary to store state and ots population
state_population = {}

# file in which population data is stored
filename = "population.txt"

# open a file for reading
with open(filename) as fh:
# read file line by line
firstLine= True
for line in fh:
if firstLine: # ignore first line
firstLine = False
continue
# split line on comma and get state and population info
(fips, state, population) = line.split(",")

# store state to population maping
state_population[state] = population


# get user input
state = input("Enter a state to get its 1997 population: ")

# check state is present in our dictionary
if state in state_population:
# print states population
print(state, " popultion in 1997 was ", state_population[state])
else: # if state is not available in dictory tell it to user
print("Try again with valid state available to me")

# copy pastable link: https://paste.ee/p/ypAbf

Sample run

$ python3 state_population.py
Enter a state to get its 1997 population: North Dakota
North Dakota popultion in 1997 was 644782

$ python3 state_population.py
Enter a state to get its 1997 population: adsadsa
Try again with valid state available to me

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote