[Tutor] need a hint

Byron Ruffin byron.ruffin at g.austincc.edu
Tue Dec 3 18:55:30 CET 2013


What I am having trouble with is finding a way to say:  if lastName appears
more than once, print something.

I ran a bit of code:
For x in lastname
  If lastname = udall
   Print something

This prints x twice.

I think what I might be hung up on is understanding the ways that I can use
a loop.  I know I need to loop through the list of names, which I have, and
set a condition dor the apppearance of a string occurring more than once in
a list but I don't know how to translate this to code.   How do I say: if
you see it twice, do something?
On 2 December 2013 02:25, Byron Ruffin <byron.ruffin at 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:


Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131203/cde78fd0/attachment.html>


More information about the Tutor mailing list