-3

I am trying to encode and then decode this function in python using cipher encryption. I wrote this function. how do I change this function so that it encodes both lower and uppercase letters?

def encrypt(message, key):
  alphabet="abcdefghijklmnopqrstuvwxyz"
  encMessage=""

  for character in message:
      if character in alphabet:
          index = alphabet.find(character)
          newPosition = (index+key)%26
          newCharacter = alphabet[newPosition]
          encMessage+=newCharacter
      else:
          encMessage+=character

  return encMessage
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

0

You can extend the alphabet with uppercase letters. You will also need to update the modulo operation to get your newPosition.

def encrypt(message, key):

  # Extend your alphabet
  alphabet="abcdefghijklmnopqrstuvwxyz"
  alphabet += alphabet.upper()

  encMessage=""

  for character in message:
      if character in alphabet:
          index = alphabet.find(character)

          # Use the correct modulo
          newPosition = (index+key) % len(alphabet)

          newCharacter = alphabet[newPosition]
          encMessage+=newCharacter
      else:
          encMessage+=character

  return encMessage
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73