Anirudha
← Work
15 Days of CodingDay 8September 17, 2025~6 min read

Functions with multiple inputs. Caesar’s Cipher

Modulo is underrated in Math

After a sleepless night of dreaming about for loops, I still decided to show up. My hypothesis for having a restless sleep is that I worked until late in the night completing the Hangman Game and couldn’t unwind properly before sleeping. So today, I decided to start early and finish earlier so that I can get a proper unwinding session done before I sleep.

Today I started with learning about functions with inputs and how we can use multiple inputs to change our function easily since its very rare to use the same function multiple times in our code with the same variables. With multiples inputs, we can define what our code should do in case of different scenarios. The syntax is intuitive but we do have to be careful of calling our function with multiple inputs and defining them according to our function. The inputs inside the function are taken from 0th position to the last and Python can’t differentiate between name, numbers, places, etc. So it is important to label the inputs in the function when you call it and that way Python understands where each input is to be used inside the function.

For the project today, I implemented Caesar's Cipher program. In old times, secret messages were encoded with a cipher using alphabets. If the key of the cipher had a value of 3, then all the alphabets were moved 3 steps ahead so alphabet A became D, B became E, C became F so on and so forth. To decode the message with a key of 3, the alphabets were moved 3 steps back. Very easy to crack the secret message if you know the key but state of the art technology in the old times.

As we have learned before, we need to break down big problems into small tasks so for me, I first decided to just encode the text given and have a fix value of the key set to 1. For this, I asked the user for a message to be encoded. I then created a function with two inputs: first input being the message and the second being the key which for us is 1 for now. To start encoding the message, I created a list with all the alphabets from “a” to “z” consecutively. Once a text was given by the user, I used a for loop inside our function to trace the position of each alphabet in our list. This gave us a number in the list of where the alphabet is, so for example, if the text given by the user is “a”, the position of that in our list is 0. Then, I just added +1 to the position because our key for now is 1. Then, I printed out this alphabet which in our example would have been “b” because 0th position +1 is equal to 1st position and the alphabet “b” is in the 1st position in our list. With me so far? This got us the very basic task of encoding done.

Now, to have different keys available for the user, I asked the user for a key value, added this in a new variable and put it in place of 1 that we were using as a key. Did you notice where we are lacking in our encoding? What happens when we have alphabet “z” which is 25th in our list and we want to move it by 2? According to our logic, the position would be 27 in the list which is empty since our list is only 25 values long. The easiest way to solve this is to add another “a” to “z” again in our list so that the 26th position would be filled by “a” and 27th position by “b” and our program won’t gives us any errors. Workable solution? Yes, but is it elegant? No. This is where the Modulo comes in. If you remember, modulo function in Python gives us a remainder value when we divide 2 numbers. So back to our example of “z” being moved by 2, the new position is 27 and if we divide this by the length of our list (=26), we get a remainder of 1. Now, what is in the 1st position in our list? Yes, correct, the alphabet “b”. That’s it, we add this one line of code to divide the desired position of the text by the length of our list and we can encode any type of message for every key value.

To decode a message, we need to move the alphabets down based on the value of the key. That means, when we have the position of the text in the list and we just subtract the value of the key, we get the desired position of the text. Again, use the modulo function to take care of the edge cases and you are good to go. This is the MVP and can be used directly.

This code can be shortened by using a single function to encode and decode a message. The user is asked if they want to encode or decode and based on their answer, an if loop is used to either run the encode part or the decode part. To add more functions, we can use another if statement for numbers, symbols, wherein if the message has a number or a symbol, to just return the same value and not to encode or decode them. Another function I added was to ask the user if they want to restart the program again in case they have more messages to encode or decode.

The code by itself is very short but it took me a really long time to come up with the logic of how to encode the message and how to take care of the edge cases. Once I figured that out, decoding was done very quickly along with adding the functions. Stackoverflow is a really great resource to go through. Even though it might not have the exact same question you need an answer to, it will at least have similar questions with which you can figure out the solution to your doubts.

Hopefully, you can create your own Caesar’s program now and if you have any questions or doubts, please reach out and I would love to help you out. Until tomorrow!