calling upper() on a string, not working?

Michal Kwiatkowski ruby at no.spam
Tue May 16 22:43:12 CEST 2006

John Salerno wrote:
> def encrypt_quote(original):
>      original_letters = filter_letters(original)

You call filter_letters() which makes upper() on all letters, so
original_letters contain only uppercase letters.

>      new_letters = list(string.ascii_uppercase)
>      while True:
>          random.shuffle(new_letters)
>          trans_letters = ''.join(new_letters)[:len(original_letters)]
>          if test_code(original_letters, trans_letters):
>              trans_table = string.maketrans(original_letters,
>              trans_letters) break
>      return original.translate(trans_table)

And here you're translating 'original' (which contains a lot of
lowercase letters) with use of trans_table that maps only uppercase
characters. This return should be:

        return original.upper().translate(trans_table)

 . o .       >>  <<
 . . o   It's easier to get forgiveness for being wrong
 o o o   than forgiveness for being right.

More information about the Python-list mailing list