Day 17 Python Caesar Cipher Pt.1

Encryption

Kenny Hin
3 min readJun 26, 2022

The goal is to create an encryption/decryption program that Caesar used back in the day to pass on notes. Wiki might do a better job of explaining it.

Fig 1 https://en.wikipedia.org/wiki/Caesar_cipher

Here is what is given:

Line 3

Direction is assigned the input of whether the user wants to encode or decode the text.

Line 4–5

Text is assigned the message the user wants to encode or decode, attached is the lower() method to make sure everything stays in consistent case bases. Shift is the number of spaces ciphertext (fig.1) is shifted according to the alphabet list — wrapped in an integer bc input from user is always a string.

My code:

This took me an hour but very proud of myself

When approaching this project, I knew i remember doing something similar grabbing individual characters in the Hangman Project. This was the code i used to help with this process.

I struggled with this code during the Hangman project so i whiteboarded it with little notes above to make sense of everything. Ended up helping me a lot for this code.

Line 9–10 / Line 24

Defined the function with parameters in line 9 and called the function on line 24.

Line 13

Knowing that we had to iterate through each letter for index and positioning, i created a for in loop with the range of the inputted text.

Line 14

Line 14 was redundant — didn’t need to define individual character for the text. Although it did act as a reminder for individual characters

Line 15

created a variable to store the alphabet index for the character. Instead of setting (char) i could have just used (position). I knew i had to define the index so that i can add it to the shift input (in the next line).

Line 16

Now its time to create the ciper index — which is simply the index of the text (index_of_char) + shift (input).

Line 17–18

To determine each individual character of the ciper word, i needed to create another variable and assign it to the alphabet list with the newly shifted index. This way, i can add each individual char to the final “ciper_text” which is defined as an empty string above.

Line 21–22

Debugging and checking

Instructors Code:

Her code was a little different then mine but had similar blocks. Her code was named a lot better.

--

--