Day 8 – Function Parameters & Caesar Cipher

  • Day 8 Goals
  • Functions with Inputs
  • Coding Exercise : Life in Weeks
  • Positional vs Keyword arguments
  • Coding Exercise: Love Calculator
  • Caeser Cipher Part 1 – Encryption
  • Caeser Cipher Part 2 – Decryption
  • Caeser Cipher Part 3 – Reorganising the code

Project – Caeser Cipher (completed)

# TODO-1: Import and print the logo from art.py when the program starts.
from art import logo
# alphabet provided as a list
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']

# function to complete the encoding/decoding
def caesar(original_text, shift_amount, encode_or_decode):
    output_text = ""
    if encode_or_decode == "decode":
        shift_amount *= -1

    for letter in original_text:
        if letter in alphabet:
            shifted_position = alphabet.index(letter) + shift_amount
            shifted_position %= len(alphabet)
            output_text += alphabet[shifted_position]
# TODO-2: What happens if the user enters a number/symbol/space?
        else:
            output_text += letter
    print(f"Here is the {encode_or_decode}d result: {output_text}")


# TODO-3: Can you figure out a way to restart the cipher program?
print(logo) # print ascii art
answer = "yes" #set the answer variable to start the while loop

# iterate through the loop as long as the user keeps typing "yes" at the end
while answer == "yes":
    direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n").lower()
    text = input("Type your message:\n").lower()
    shift = int(input("Type the shift number:\n"))
    caesar(original_text=text, shift_amount=shift, encode_or_decode=direction)
    answer = input("Press 'yes' to restart or type 'no' to exit.\n")

# print a closing message
print("Thank you for using Caesar's Cipher. Beware the Ides of March")