[Tutor] question about the an exercise in <core python programing> (including the discription about the exercise)

Alan Gauld alan.gauld at btinternet.com
Wed Jan 4 18:44:25 CET 2012

On 04/01/12 14:54, Joel Goldstick wrote:

>>>     two files' content is equal, just print "equal". Else, print the
>>>     rows And column number of the first different position.

>> while True:
>>    line = f1.readline()
>>    if line != f2.readline():
>>         print lineNum, " : ", line
> # in here I believe you need a bit more.  You now know the lines are
> not equal, but the exercise requires the line number and column number

Oops, I didn't notice the column bit...sorry.

> # To do that, you need to do something similar to what Alan has shown
>      for i, c in enumerate(line):      #enumerate iterates over the
>            if line2[i] != c:                  # line2[i] is the
>                print "The file differs at line %d, column %d" % lineNum, i

Yep, that ought to work.

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list