[Tutor] trouble with function-- trying to check differences btwn 2 strings

zannah marsh zannah.m at gmail.com
Tue Mar 6 18:39:51 CET 2007


what I was trying to do with that loop is check each character in the string
against the corresponding character at the same position in the second
string. rikart pointed out that my loop was actually checking if that
character exists anywhere in the second string.
basically, in pseudocode:

for thing in bunch of things
    check to see if thing  from bunch of things is present in second bunch
of things

so "thing" here (called item in my original code) does not represent the
index. it's just a counter for things.
in java (which is the only language i am even kind of familar with) loops
and iterators are different, and everything has to be defined...

rikart pointed out that you need to use a range to get to the indicies of
the items in the string.

for item in range(len(string))...
     if word1[item] == word2[item]


and that's the fix. whether i am doing something totally weird, or that
could be done a better way in python, i don't know...


zannah



On 3/6/07, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>
> "David Perlman" <dperlman at wisc.edu> wrote
>
> > I can't figure out how this would ever work at all.  It seems like
> > it's either checking to see whether boolean TRUE is in word2, or
> > else
> > it's checking to see whether item is equal to boolean TRUE or FALSE,
> > and neither of those should ever be true.
>
> It's doing the latter and since anything that's not 'empty' in
> Python evaluates to true we wind up checking whether
> true == (item in word)
>
> So if the item is in word we get true == true which is true.
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
> _______________________________________________
> Tutor maillist  -   Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070306/16f56f5b/attachment.htm 


More information about the Tutor mailing list