Skip to main content

Command Palette

Search for a command to run...

Day 4: cipher wrap around

Published
1 min read
Day 4: cipher wrap around
A

Hi there! I’m a passionate learner who loves exploring Python's endless possibilities. Over the past years, I’ve immersed myself in coding, honing my skills by tackling diverse projects that combine creativity and problem-solving.

From building a modern rendition of the classic Asteroids game in Python to experimenting with data visualization and automation, I thrive on turning ideas into reality through code.

Follow along as I learn, grow, and share my journey!

pexels-yaroslava-borz-10153595.jpg Today, I started to implement the Caesar Cipher. The first part was to create a function called 'encrypt' that takes the 'text' and 'shifts' the input text.

def encrypt(text, shift):

The actual shift can be accomplished with an array new_text holding each letter shifted by shifts places.

    new_text = []
    for letter in text:
        index = alphabet.index(letter)

figuring out the index then its easy enough adding the shift to index index+shifts

But the issue with this is as you approach letters deeper in the alphabet you'll be going out of range. So you have to wrap around the alphabet - and the best way to do that is by using the modulus operator to always check to see what this value is modulus the letters in the alphabet.

    new_text.append(alphabet[(index+shift)%len(alphabet)])

and by joining the new_text array you end up with the shifted encoded message.

print(''.join(new_text))

Tomorrow, I'll use this to continue the Caesar Cipher.

#100DaysOfCode

Follow me on Twitter at AllieNicole903