# Report the District of a Congressional Representative
# Using a dictionary.

# Reads the input file and puts relevant data into a dictionary
def readRepFile():
    repDict = {} # start with the empty dictionary
    
    repFile = open("reps.txt","r")

    # skip 3 lines
    for i in range(0,3):
        repFile.readline()

    # picks up on 4th line of file
    for line in repFile:
        words = line.split("\t") # split at tab characters
        repName = words[1] # the name is in position 1
        nameList = repName.split(",") #list w/ last,first
        lastName = nameList[0]
        dist = words[0]  # district is first item on line
        repDict[lastName] = dist # key is lastName, value is district

    repFile.close() # close the file when done

    return repDict  # the dictionary is the output of the function

# Get a name from the user
def getName():
    nameS = input("Enter rep name: ")
    return nameS

# function that gets a name from the user and then
# reports the District for that Representative if
# this is a name that is in the Dictionary
# Does it 5 times
def reportRep(repDict):

    for i in range(0,5):
        # ask the question 5 times
        name = getName()  # use our function to get name
        if name in repDict: # check if it is in dictionary
            # always check before using a dictionary key
            # to avoid program crashing on bad key
            dist = repDict[name] # get the value for that key
            print("Representative",name,"serves District",dist)
        else:
            print("There is no representative",name)

    return


def main():
    # read file and put relevant data in list
    repDict = readRepFile()
    print(repDict)

    # answer questions based on list
    reportRep(repDict)

# The only line outside a function definition!!!
# Follow this rule!!!
# PS from...import... lines should also be outside function definitions,
# at the top of the file
main()
