Reposting to the list. Please send your response to the tutor list rather than directly to me. That way you'll get a response more quickly (from someone else). Also can you please write your response below mine like below (rather than top-posting)?
On 3 December 2013 06:25, Byron Ruffin <byron.ruf...@g.austincc.edu> wrote: > On Mon, Dec 2, 2013 at 4:51 AM, Oscar Benjamin <oscar.j.benja...@gmail.com> > wrote: >> >> On 2 December 2013 02:25, Byron Ruffin <byron.ruf...@g.austincc.edu> >> wrote: >> > >> > 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. >> >> You're currently doing this: >> >> > senateInfo = {} >> > senateInfo[lastName] = state >> >> Instead of storing just a state in the dict you could store a list of >> states e.g.: >> >> senateInfo[lastName] = [state] >> >> Then when you find a lastName that is already in the dict you can do: >> >> senateInfo[lastName].append(state) >> >> to append the new state to the existing list of states. You'll need a >> way to test if a particular lastName is already in the dict e.g.: >> >> if lastName in senateInfo: > > I tried this but I don't understand how to use it. I'm a beginner. I > understand that the brackets puts the states into a list but I don't know > what to do with that. I am able to detect if a name appears more than once > using an "if" but that code runs as many times as the name occurs. Could you perhaps show the code that you currently have? I don't quite understand what you mean. Note that I deliberately didn't give you an exact answer to your problem because it looks like homework. Oscar _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor