Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Your mode of life will be changed for the better because of new developments.


comp / comp.lang.python / Re: Flubbed it in the second interation through the string: range error... HOW?

SubjectAuthor
o Re: Flubbed it in the second interation through the string: range error... HOW?MRAB

1
Subject: Re: Flubbed it in the second interation through the string: range error... HOW?
From: MRAB
Newsgroups: comp.lang.python
Date: Wed, 29 May 2024 11:38 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: python@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: Flubbed it in the second interation through the string: range
error... HOW?
Date: Wed, 29 May 2024 12:38:17 +0100
Lines: 71
Message-ID: <mailman.58.1716982886.2909.python-list@python.org>
References: <1901134155.5453771.1716957203090.ref@mail.yahoo.com>
<1901134155.5453771.1716957203090@mail.yahoo.com>
<7a6a2d11-6035-46d3-a078-ecff01909fe9@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de jyOomYRz5XywMX7IA1NT/wMS39ZJkd2/q2vRA4s5itZQ==
Cancel-Lock: sha1:EgfMD5QnyDePek1B8QKIf0CTn9M= sha256:PwTSFlVHR6jRde1mOukvKHYYgnu71yq1351+AMOltpY=
Return-Path: <python@mrabarnett.plus.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=plus.com header.i=@plus.com header.b=N4KGvPRC;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'def': 0.04;
'traceback': 0.04; '(most': 0.05; 'string': 0.07; 'converting':
0.09; 'elif': 0.09; 'from:addr:python': 0.09; 'meant': 0.09;
'received:192.168.1.64': 0.09; 'string,': 0.09;
'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16;
'iteration': 0.16; 'iteration,': 0.16; 'lowercase': 0.16;
'message-id:@mrabarnett.plus.com': 0.16; 'received:plus.net':
0.16; 'subject:second': 0.16; 'subject:string': 0.16; 'wrote:':
0.16; 'to:addr:python-list': 0.20; 'code': 0.23; 'skip:{ 20':
0.26; 'error': 0.29; 'header:User-Agent:1': 0.30; 'think': 0.32;
"doesn't": 0.32; 'letter,': 0.32; 'python-list': 0.32;
'received:192.168.1': 0.32; "i'm": 0.33; 'header:In-Reply-To:1':
0.34; 'following': 0.35; 'cell': 0.36; "it's": 0.37;
'received:192.168': 0.37; '8bit%:14': 0.38; 'block': 0.39;
'lower': 0.62; 'received:212': 0.62; 'skip:m 20': 0.63; 'pass':
0.64; 'subject:through': 0.69; '"when': 0.84; 'subject: \n ':
0.84; 'you:': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1716982698; bh=2L+Hsaks/hiPjoz4eDp6FuSnRun9Pz5/cbmAEHZbc2Q=;
h=Date:Subject:To:References:From:In-Reply-To;
b=N4KGvPRCQcYHHm7R/OUPZibMdC7m5ZqmoIlyZE6clNv2XVECO0jAA37NbraZAYI5T
DNwLV3A2Ijj5KUW1HGoV4Il07WUZNUFzz3RO1E+A7rvwSx6Fe6JXe39pHW9p65d6Kf
/wmFipQ6FaObDS+NXOf9qC1O4ZS6YVzUJ4qxrkfCJFvcomWL5ByNHDtf5umMEDVqNB
sB5yfSHDvIZuecXUUE3YVGg7Nw6H8AcaHDGte1xgYbfsJnU2XIz59DvT+OVKgBDmLA
UBMAj/mEfNav6RULHYTxAfNT9UaXQHHV8uz6ko8yhYf6y1om+qsGDt13Seet1yLHNf
/XEcEpE8dh5sw==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=NPx2+F6g c=1 sm=1 tr=0 ts=665713aa
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=lLJAOk_WU69A3Ky1:21 a=IkcTkHD0fZMA:10 a=TJxWpaKTWpE5yFEM2EgA:9
a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla Thunderbird
Content-Language: en-GB
In-Reply-To: <1901134155.5453771.1716957203090@mail.yahoo.com>
X-CMAE-Envelope: MS4xfLkBXZbdaRJ5wkPdtFWwzBSRMUTL190ItiRG8cXcoHwQTb9/uF8mqxoacUg+2GHSuFlW5A40Sp0MC5ZeISWo6oMLwSB9axQfaji496gSmlgF7706QTtv
YFRkmyxYNPkUad3x9Tchi6EsXpLKbRa3wZoIpVDCqsVNMR9xlDzuZG7g3logs8aKuS7H+xz13jj06qYXxShOED/oDaVrsunE+Bo=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <7a6a2d11-6035-46d3-a078-ecff01909fe9@mrabarnett.plus.com>
X-Mailman-Original-References: <1901134155.5453771.1716957203090.ref@mail.yahoo.com>
<1901134155.5453771.1716957203090@mail.yahoo.com>
View all headers

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]`.

1

rocksolid light 0.9.8
clearnet tor