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

zannah marsh zannah.m at gmail.com
Tue Mar 6 00:04:24 CET 2007


I am very new to Python (I've been learning it for about 2 weeks, in a
class). I wrote this function ( as part of a larger program, for my
homework) which is supposed to step through two given strings and check for
differences between the strings. It returns a list of the indices at which
the characters are different. Here it is:

def getDiff(word1,word2):
    #this function takes two words and returns a list containing the index
of each point of difference in the words
    difList = [] #creates empty list to hold index (location) of differences
in words
    index = 0 #sets variable index to 0 to start
    for item in word1: #steps throug string
        if item == item in word2: #checks characters against each other
            print "there is a match at index ", index
            print word1[index]
            print word2[index]
            index += 1 #increments index

        else:
            print "they are not the same at index ", index
            print word1[index]
            print word2[index]
            difList.append(index) #adds index value to list
            index += 1 # increments index
    return difList

It works, but only sometimes-- in some cases, it evaluates letters that are
different as being the same. Seems to be happening after a run of a few
matching letters? It also works for "carpet" "carrot", but not for "carrot"
"carpet". I think it must be something wrong with the structure of the
loop-- or how i am using it-- but I can't see it. Does anyone have any
suggestions? Here is output from some sample runs:

>>> getDiff("desk","foot") # works fine
they are not the same at index  0
d
f
they are not the same at index  1
e
o
they are not the same at index  2
s
o
they are not the same at index  3
k
t
[0, 1, 2, 3]
>>> getDiff("beep","bees") # works fine
there is a match at index  0
b
b
there is a match at index  1
e
e
there is a match at index  2
e
e
they are not the same at index  3
p
s
[3]
>>> getDiff("freeze","fresia")  # this one did not work!
there is a match at index  0
f
f
there is a match at index  1
r
r
there is a match at index  2
e
e
there is a match at index  3
e
s
they are not the same at index  4
z
i
there is a match at index  5
e
a
[4]
>>> getDiff("carrot","carpet") # this one did not work!
there is a match at index  0
c
c
there is a match at index  1
a
a
there is a match at index  2
r
r
there is a match at index  3
r
p
they are not the same at index  4
o
e
there is a match at index  5
t
t
[4]
>>> getDiff("carpet","carrot") #but this does work (inverted order from
above)
there is a match at index  0
c
c
there is a match at index  1
a
a
there is a match at index  2
r
r
they are not the same at index  3
p
r
they are not the same at index  4
e
o
there is a match at index  5
t
t
[3, 4]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070305/9b629546/attachment.html 


More information about the Tutor mailing list