# Answer district queries
# Reads file CAreps.txt

repDict = {}  # The empty dictionary
district = 1 # Keeps track of which district we are working on
repFile = open('CAreps.txt','r')
line = repFile.readline()

while line != '':
    words = line.split() # words is a list of words
    name = ''
    for i in range(2,len(words)):
        name = name+' '+words[i]  # Construct name
    lastName = words[len(words)-1]  # The last item in the list
    repDict[lastName] = district # key = lastName, value = district
    district = district+1 # move on to next district
    line = repFile.readline()  # move on to next line
repFile.close()
# print repDict # commented out 

reply = 'Garbage'
while reply != '':
    reply = raw_input('Enter a last name, or enter to exit: ')
    if reply != '':
	# The in operator, with a dictionary, checks to see if
	# a string is the key of an item in the dictionary.
	# If you try to index a string that is not there, the program
	# crashes.
        if reply in repDict:
            district = repDict[reply]
            print 'Representative',reply,'serves district',district
        else:
            print 'There is no Representative',reply

    
    

