# [Tutor] a function I fail to understand

Marc Tompkins marc.tompkins at gmail.com
Tue Mar 22 04:24:41 CET 2011

```On Mon, Mar 21, 2011 at 6:06 PM, David <ldl08 at gmx.net> wrote:

> Hello list,
>
> I am having trouble understanding the following function. What trips me
> up is the "letter = letter.lower()" line.
>
> As I understand, the function takes a letter and assigns True to a
> letter if it is upper case.
>
> But then he goes to
>
> letter = letter.lower()
>
> and all letters are converted back to lower again!?? The point is that,
> to my understanding, the logic follows from the first block to
> letter = letter.lower(). Isn't that true?
>
> Thanks for helping me out,
>
> David
>
>
> def rotate13_letter(letter):
>    """
>    Return the 13-char rotation of a letter.
>    """
>    do_upper = False
>    if letter.isupper():
>        do_upper = True
>
>    letter = letter.lower()
>    if letter not in CHAR_MAP:
>        return letter
>
>    else:
>        letter = CHAR_MAP[letter]
>        if do_upper:
>            letter = letter.upper()
>
>    return letter
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

The person who wrote this only wanted to write one conversion routine, so
what he's doing is:
- check to see if the letter is uppercase, and if it is, set a flag
(do_upper) for later
- force the letter to lowercase regardless of its previous status
- do the conversion
- if the do_upper flag is set, convert back to uppercase
- return the letter

It may look a little funny, but it beats writing logic to deal with each
upper- and lower-case letter.
-------------- next part --------------
An HTML attachment was scrubbed...
```