I have a problem with the lesson about indexes
the tasks is to name the fifth letter of the word “MONTY” but there is no 5 letter because its said that i have to count from 0 instead of 1 while the answer for this task is "Y"
Could someone explain why?
Lists and strings are zero-indexed, so remember that the first element is index.
It goes to offsets. The first element is offset from the left edge by zero elements. The last element is immediately to the left of its right edge index.
A string of length 5 has index at its right edge. It is not included in the string.
0 1 2 3 4 5 M O N T Y
My best solution that I can come up with is that if you do index(5) (or whatever the line of code is, I don’t remember fully) it will just do the last letter
This is my best solution, and probably not the solution
The code comes out as “MONTY” because the fifth letter is not the fifth index. I missed this and had to have the code given to me but the tip on the side says “Remember the fifth letter is not at index 5” meaning that it literally wants you to assign the fifth letter to the string fifth_letter meaning that you have to change the way you look at it. Index 4 - “MONTY” - puts the fifth letter ( Y ) into the string designation of fifth_letter. I’m new at this so if my explanation is clunky, I apologize. This is my first attempt at answering someone else’s question.
There are a number of ways to access the last letter of a string.
is the obvious one, but these will also work…
"MONTY"[len('monty') - 1] # see the connection to above? "MONTY"[-1] # reverse index (right to left)
This is either absolute nonsense or I am dumb.?! This is my first day in Python. Maybe I am missing something, but the way it is explained puzzles me.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.