[Tutor] need a hint
Byron Ruffin
byron.ruffin at g.austincc.edu
Mon Dec 2 03:25:10 CET 2013
The following program works and does what I want except for one last
problem I need to handle. The program reads a txt file of senators and
their associated states and when I input the last name it gives me their
state. The problem is "Udall". There are two of them. The txt file is
read by line and put into a dictionary with the names split. I need a
process to handle duplicate names. Preferably one that will always work
even if the txt file was changed/updated. I don't want the process to
handle the name "Udall" specifically. For a duplicate name I would like
to tell the user it is not a unique last name and then tell them to enter
first name and then return the state of that senator.
Thanks
An excerpt of txt file...
Arkansas Mark Pryor (D) 2003 2015
Arkansas John Boozman (R) 2011 2017
California Dianne Feinstein (D) 1992 2019
California Barbara Boxer (D) 1993 2017
Colorado Mark Udall (D) 2009 2015
Colorado Michael F. Bennet (D) 2009 2017
def createList(state):
senateInfo = {}
info = open( "USSenators.txt", "r" )
for line in info:
dataOnLine = line.split( "\t" )
state = dataOnLine[ 0 ]
senator = dataOnLine[ 1 ]
nameSplit = dataOnLine[ 1 ].split(" ")
if len(nameSplit) == 3:
lastName = nameSplit[1]
elif len(nameSplit) == 4:
lastName = nameSplit[2]
senateInfo[lastName] = state
info.close()
return senateInfo
def test( senator, usSenators ):
print( usSenators[senator] )
def main():
usSenators = createList( "USSenators.txt" )
senator = input("Enter last name of Senator")
test(senator, usSenators )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131201/7f3787a4/attachment.html>
More information about the Tutor
mailing list