Skip to main content

Command Palette

Search for a command to run...

Day 6: Caesar Cipher Complete

Published
1 min read
Day 6: Caesar Cipher Complete
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!

Today, I refactored the Caesar Cipher to completeness.

caesar-marble.jpg

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))

def caesar(text, shift, direction):
  new_text = []
  if direction == 'decode':
    shift *= -1
  for letter in text:
    index = alphabet.index(letter)
    new_position = (index + shift) % len(alphabet)
    new_text.append(alphabet[new_position])
  print(''.join(new_text))

caesar(text, shift, direction)

All the pieces are incorporated into this one function that can either encode or decode.

Tomorrow, I'll start looking at Dictionaries.

#100DaysOfCode

Follow me on Twitter at AllieNicole903