csev/py4e

In Strings chapter, line 99 is a bug, "banana" should be "fruit" and index should be 5..

MikePfunk28 opened this issue · 6 comments

Bug on line 99, "banana" should be replaced by "fruit" and index 6 should be indec 5 which means we start at 0 and go to 4. I made the corrections below, I have not submitted a bug as of yet.

>>> length = len(fruit)
>>> last = fruit[length]
IndexError: string index out of range

The reason for the IndexError is that there is no letter in
"fruit" with the index 5. Since we started counting at
zero, the six letters are numbered 0 to 4. To get the last character,
you have to subtract 1 from length:

csev commented

Thanks. All set.

In the text, fruit refers to the variable fruit, which was set to the string literal 'banana'. If the above changes are to be implemented, then the len() function should be passed the string literal 'fruit' and not the variable fruit.

I forked the repo, and submitted the correction back to the variable fruit (i.e. 'banana').

(pdf pg 68 of text, section 6.2 Getting the length of a string using len; Original text version with typos in bold)
len is a built-in function that returns the number of characters in a string:

>>> fruit = 'banana'
>>> len(fruit)
6

To get the last letter of a string, you might be tempted to try something like this:

>>> length = len(fruit)
>>> last = fruit[length]
IndexError: string index out of range

The reason for the IndexError is that there is no letter in “fruit” with the index 5. Since we started counting at zero, the six letters are numbered 0 to 4. To get the last character, you have to subtract 1 from length:

(pdf pg 68 of text, section 6.2 Getting the length of a string using len; Corrected version)
len is a built-in function that returns the number of characters in a string:

>>> fruit = banana
>>> len(fruit)
6

To get the last letter of a string, you might be tempted to try something like this:

>>> length = len(fruit)
>>> last = fruit[length]
IndexError: string index out of range

The reason for the IndexError is that there is no letter in fruit with the index 6. Since we started counting at zero, the six letters are numbered 0 to 5. To get the last character, you have to subtract 1 from length:

Yes I read that wrong, I was isolating the problem when I was working on it, thinking it did not carry. After reading your comment it makes sense, thanks!. I wonder if this has been corrected because my version is showing the incorrect version.

Is this fixed? Or maybe the update has not been committed yet? In my version it has the incorrect information, like you pointed out jtspinks, did this get updated, is your version off as well? Good thing you caught that, I was working on a problem and thinking to myself, I messed this up somewhere. I think this was it, I reopened for reference.

I corrected it and submitted the PR. I think it just has not updated yet.

csev commented

So this got merged into github pretty quickly - the English PDF online updates every 30 minutes. But the HTML on www.py4e.com takes a manual step by me - which I have now done. So it should be right in all of my copies except the actual print book on Amazon which I update about once per year. Let me know if there is a copy where this fix is not yet represented.