[Tutor] beginning encryption

Steven D'Aprano steve at pearwood.info
Thu Apr 19 08:46:46 EDT 2018


Hi Roger, and welcome. See my comments below.


On Wed, Apr 18, 2018 at 04:39:27PM -0700, Roger Lea Scherer wrote:

> def encryptor(address):
>     encrypted = ""
>     for char in address:
>         if char != alphabet:
>             encrypted += char
>         else:
>             pos = alphabet.index(char)
>             encrypted += encryption[pos]
>     print(encrypted)

For each character in the Gettysburg address, you compare the individual 
single character "F", "o", "u", "r", etc against the ENTIRE alphabet 
"abcde...xyz".

Since a single letter is never equal to 26 letters, you always get

    char != alphabet  # they are never equal

and so you always add the char unchanged to the encrypted text.

Try this instead:

    if char not in alphabet:
        encrypted += char

Then once you have that working, write back (with a shorter extract of 
the Gettysburgh address please!) and we'll see how else we can improve 
your code.



-- 
Steve


More information about the Tutor mailing list