Flubbed it in the second interation through the string: range error... HOW?

MRAB python at mrabarnett.plus.com
Wed May 29 07:38:17 EDT 2024


On 2024-05-29 05:33, Kevin M. Wilson via Python-list wrote:
> The following is my effort to understand how to process a string, letter, by letter:
> def myfunc(name):        index = 0    howmax = len(name)    # while (index <= howmax):    while (index < howmax):        if (index % 2 == 0):            print('letter to upper = {}, index {}!'.format(name[index], index))            name = name[index].upper()            print('if block {} and index {}'.format(name[index], index))        elif (index % 2 > 0):            print(index)            print('Start: elseif block, index is {}, letter is {}'.format(index, name))            # print('letter to lower = {}'.format(name[index]))            # print('Already lowercase do noting: name = {}'.format(name[index]))        index += 1        # index = name.upper()
>      return name
> myfunc('capitalism')
> Error message:                        Not making sense, index is 1, letter s/b 'a'letter to upper = c, index 0!
> if block C and index 0
> 1
> Start: elseif block, index is 1, letter is C
> ---------------------------------------------------------------------------
> IndexError                                Traceback (most recent call last)
> Cell In[27], line 21
>       17         # index = name.upper()
>       19     return name
> ---> 21 myfunc('capitalism')
> 
> Cell In[27], line 8, in myfunc(name)
>        6 while (index < howmax):
>        7     if (index % 2 == 0):
> ----> 8         print('letter to upper = {}, index {}!'.format(name[index], index))
>        9         name = name[index].upper()
>       10         print('if block {} and index {}'.format(name[index], index))
> 
> IndexError: string index out of range***************************************************
> So, I'm doing something... Stupid!!
> ***************************************************
> "When you pass through the waters, I will be with you: and when you pass through the rivers, they will not sweep over you. When you walk through the fire, you will not be burned: the flames will not set you ablaze."
> Isaiah 43:2

I think the code is this:

def myfunc(name):
     index = 0
     howmax = len(name)
     # while (index <= howmax):
     while (index < howmax):
         if (index % 2 == 0):
             print('letter to upper = {}, index {}!'.format(name[index], 
index))
             name = name[index].upper()
             print('if block {} and index {}'.format(name[index], index))
         elif (index % 2 > 0):
             print(index)
             print('Start: elseif block, index is {}, letter is 
{}'.format(index, name))
             # print('letter to lower = {}'.format(name[index]))
             # print('Already lowercase do noting: name = 
{}'.format(name[index]))
         index += 1
         # index = name.upper()
     return name

myfunc('capitalism')


What is:

     name = name[index].upper()

meant to be doing?

What it's _actually_ doing is getting the character at a given index, 
converting it to uppercase, and then assigning it to `name`, so `name` 
is now 1 character long.

It doesn't this when 'index' is 0, so after the first iteration, `name` 
is a single-character string.

On the second iteration it raises IndexError because the string is only 
1 character long and you're asking for `name[1]`.



More information about the Python-list mailing list