On Tue, Dec 03, 2013 at 09:51:12PM -0600, Byron Ruffin wrote:
> I realize the code snippet was bad.  It was meant to be pseudo code.  I was
> on my phone and far from pc. Anyway....
> 
> I tried this:
> 
> already_seen = set()
> for name in last_names:
>     if name in already_seen:
>         print("Already seen", name)
>     else:
>         already_seen.add(name)

No you didn't. In the code below, you don't use this *exact* code. I'm 
sorry to keep harping on this point, but to be a successful programmer 
you must be *precise and accurate*. If you think I'm annoyingly 
pedantic, that is nothing compared to how pedantic the computer is. The 
computer can not and will not think "Oh, I know what he actually means". 
It will do exactly what you tell it to do, whether you want it to or 
not.

The critical factor here is that in my example, you loop over the 
collection of names (note plural), while in your code you loop over each 
individual name itself. See below for more detail.

 
> I am not seeing a pattern in the output to give me a clue as to why it is
> doing this.  Also, it seems to be referencing chars when variable lastName
> is an item in a list.

Notice that one variable is called "last_names" and the other is called 
"lastName". One refers to *plural names*, the other refers to a single 
name.

Of course, that difference is only meaningful to us, the human reader or 
writer. The computer doesn't care, you could call the variable "xvhdkwy" 
for all the difference it makes. But we know the difference between 
"last names" and "last name", and we are responsible for ensuring that 
something called "lastName" is a single last name, and something called 
"lastNames" is a collection of last names, perhaps a list.

When you loop over a list of names, you get each name individually. If 
you look over a single name, you get each letter individually:

for thing in "Smith":
    print(thing)

will give:

S
m
i
t
h


Here's a snippet from your code:

>         already_seen = set()
>         for name in lastName:
>             if name in already_seen:
>                 print("Already seen", name)
>             else:
>                 already_seen.add(name)

This is no good, because you're already looping over the names, by 
starting another loop here you're looping over the characters in the 
name. Instead, move the line:

already_seen = set()

outside of the loop, and change the inner loop to not be a loop at all:

        if lastName in already_seen:
              print("Already seen", lastName)
        else:
              already_seen.add(lastName)




Something like that ought to work.



-- 
Steven
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to