I want understand how this word wrap program playing on input

MRAB python at mrabarnett.plus.com
Thu Apr 4 20:50:40 EDT 2019


On 2019-04-04 20:48, David Raymond wrote:
>> Yep, spotted that too! :-) BTW, your fix also a bug: the last word on a
>> line won't be followed by a space (off-by-one). The easiest fix for that
>> is to add 1 to line_length initially, another little trick.
> 
>> Or, equivalently, to reset current_line_length to -1, which is an elegant hack.
> 
> I'm actually going to stand my ground on this one. The incrementing of the line length happens _after_ you've added that word to the line, and the check for length is checking against the length of the new word. So adding the 1 for "the space at the end" won't come into effect until the next word, where you're asking "if I add this new word..." at which point you'll want to have included that space.
> 
> 
> So copy and paste wrap to wrap2 and make your changes to wrap2, then run something like this, playing with the line length to check. The pipes show the last allowed position of a character.
> 
[snip]
Oops! You're correct.


More information about the Python-list mailing list