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

Liste des GroupesRevenir à cl python 
Sujet : Re: Flubbed it in the second interation through the string: range error... HOW?
De : list1 (at) *nospam* tompassin.net (Thomas Passin)
Groupes : comp.lang.python
Date : 29. May 2024, 15:32:39
Autres entêtes
Message-ID : <mailman.64.1716993171.2909.python-list@python.org>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On 5/29/2024 8:55 AM, Kevin M. Wilson wrote:
Please recall, I said the format for the email failed to retain the proper indents.
I'll attach a picture of the code!
Purpose; to uppercase every other letter in a string.
 Thanks all, KMW
Simpler is good, and readability is good.  For a simple conversion that has a little touch of generality:
s1 = 'this is a test'
def convert(i, ch):
     return ch.upper() if i % 2 else ch
result = ''.join([convert(i, ch) for i, ch in enumerate(s1)])
print(result)  # tHiS Is a tEsT
However, this has a weakness: what to do about spaces.  Should they be counted among the characters to be uppercased? or should they not be included in the count of the alternation?  If you want to uppercase every other letter that is not a space, things become a little more complicated.  And then do you want this to apply to all whitespace or only spaces?
If you want to skip changing spaces, then you need to track the state of converted characters in some way.  It would probably be easier (and more readable) to use a "for x in t:" construction:
def convert(convert_this, ch):
     """Convert character ch if convert_this is True.
     Don't convert spaces.
     """
     if convert_this:
         if ch == ' ':
             return (convert_this, ch)
         elif convert_this:
             return (False, ch.upper())
     return (True, ch)
convert_next = False
result = ''
for ch in s1:
     convert_next, ch = convert(convert_next, ch)
     result += ch
print(result)  # tHiS Is A TeSt
There could be even more complications if you allow non-ascii characters but you were asking about processing character by character so I won't get into that.
(You haven't specified the problem in enough detail to answer questions like those).

***************************************************
"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
*
  On Wednesday, May 29, 2024 at 06:19:56 AM MDT, Thomas Passin via Python-list <python-list@python.org> wrote:
  On 5/29/2024 3:14 AM, Chris Angelico via Python-list wrote:
 > On Wed, 29 May 2024 at 16:03, Cameron Simpson via Python-list
 > <python-list@python.org <mailto:python-list@python.org>> wrote:
 >> By which Thomas means stuff like this:
 >>
 >>      print(f'if block {name[index]} and index {index}')
 >>
 >> Notice the leading "f'". Personally I wouldn't even go that far, just:
 >>
 >>      print('if block', name[index], 'and index', index)
 >>
 >> But there are plenty of places where f-strings are very useful.
 >
 > I wouldn't replace str.format() everywhere, nor would I replace
 > percent encoding everywhere - but in this case, I think Thomas is
 > correct. Not because it's 2024 (f-strings were brought in back in
 > 2015, so they're hardly chronologically special),
 I only meant that they have been around for 9 years and are usually more
readable, so just change over already.  I had some inertia over them
myself (imagine sticking with % formatting!) so I understand.
   > but because most of
 > this looks like debugging output that can take advantage of this
 > feature:
 >
 > print(f"if block {name[index]=} {index=}")
 >
 > ChrisA
 -- https://mail.python.org/mailman/listinfo/python-list <https://mail.python.org/mailman/listinfo/python-list>

Date Sujet#  Auteur
29 May 24 o Re: Flubbed it in the second interation through the string: range error... HOW?1Thomas Passin

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal