# Reading a file, and putting the data into a dictionary
# Reads the file "StatePopulations.txt", which we used in the last lecture

# Open the file for reading
stateFile = open("StatePopulations.txt","r")

# Begin with the empty dictionary
popDict = {}
for line in stateFile:
    # split on whitespace; wordList is now a list of words
    wordList = line.split()
    # Ignore empty lines
    if wordList == []:
        continue
    # Ignore territories
    if wordList[0] != "n/a":
        if len(wordList) == 11:
            state = wordList[2]
            population = wordList[3]
        else:
            state = wordList[2]+" "+wordList[3]
            population = wordList[4]
	# Store the population as the value, with stateName as the key
        popDict[state] = population

while True:
    query = raw_input("Enter a state, or return to exit: ")
    if query == "":
        break
    # Check to see if state the user wants is in the database
    if query in popDict:
	# extract the data from the database
        print "The population of",query,"is",popDict[query]
    else:
        print "There is no state called",query


stateFile.close()


    
