Hi Ben,

Yes, that's the point: Python doesn't know when to stop.  *grin*

The way we've rewritten the loop:

    while True:
        ...

is an "infinite" loop that doesn't stop unless something in the loop's
body does something extraordinary, like "breaking" out of the loop.
Python is much dumber than we might expect.


In more detail: Python's readline() method doesn't fail when we reach the
end of a file:  we actually start hitting the empty string.  For example:

######
>>> import StringIO
>>> sampleTextFile = StringIO.StringIO("""This is
... a sample
... text file
... """)
>>> sampleTextFile.readline()
'This is\n'
>>> sampleTextFile.readline()
'a sample\n'
>>> sampleTextFile.readline()
'text file\n'
>>> sampleTextFile.readline()
''
>>> sampleTextFile.readline()
''
######

Notice that when we hit the end of the file, readline() still continues to
run and give us empty string values.  That's why the loop above needs to
make sure it breaks out in this particular situation.


Does this make sense?  Please feel free to ask questions about this.

Yes The  -- while true loop --  needs something to be false or it needs to be told when to stop. If that is correct, then it makes sense.

Thanks
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to