I'm writing a Caesar Cipher code for a part of a controlled assessment. I built a fully functioning program and I thought I had it sussed but after changing a few things around I went to check back and everything has gone wrong!
The code's quite untidy but I'm getting a bit sick of coding this now and have taken to the internet to get someone else's view.
Code:
answer ="C"
while answer == "C":
lettersList=['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','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']
def menu():
userChoice=input("Would you like to encrypt or decrypt a message? E or D.\n").lower()
while userChoice != "e" and userChoice != "d":
print("Invalid.")
userChoice=input("Would you like to encrypt or decrypt a message? E or D.\n").lower()
print("\n")
return userChoice
def getPlaintext():
plaintext= input("Please enter the message you would like encrypted/decrypted\n").lower()
while plaintext.isalpha() == False:
print("Invalid")
plaintext=input("Please enter the message you would like encrypted/decrypted\n").lower()
print("\n")
return plaintext
def getKey():
key=int(input("Please enter a key. 1-26\n"))
while key > 26 or key < 1:
print("Invalid.")
key=int(input("Please enter a key. 1-26\n"))
print("\n")
return key
def encryptText(plaintext,key):
characterNumber = 0
newMessage = ""
for characters in plaintext:
character = plaintext[characterNumber]
characterPosition = lettersList.index(character)
newPosition=character+key
newLetter = lettersList[newPosition]
newMessage = (newMessage+newLetter)
characterNumber= characterNumber+1
print(newMessage)
def decryptText(plaintext,key):
characterNumber = 0
newMessage = ""
for characters in plaintext:
character = plaintext[characterNumber]
characterPosition = lettersList.index(character)
print(characterPosition)
newPosition=characterPosition-key
newLetter = lettersList[newPosition]
newMessage = (newMessage+newLetter)
characterNumber= characterNumber+1
newMessage = (newMessage.lower())
print(newMessage)
userChoice=menu()
plaintext=getPlaintext()
key=getKey()
if userChoice == "e":
encryptText(plaintext,key)
elif userChoice == "d":
decryptText(plaintext,key)
print(newMessage)
You've got some simple misspellings in your code which prohibit the correct functioning. for characters in plaintext:
should be for character in plaintext:
- in your code you create a new variable characters
which is not used anywhere.
The correct encrypting function with comments looks like this:
def encryptText(plaintext, key):
newMessage = ""
for character in plaintext: # 'character' holds each letter from 'plaintext'
characterPosition = lettersList.index(character)
newPosition = characterPosition + key # shift the index, not the character!
newLetter = lettersList[newPosition]
newMessage = newMessage + newLetter # append new letter to new message
print(newMessage)
To make this work correctly on lettersList
with just one occurrence of each character (i.e. of length 26) you need to check that the shifted index is not out of bounds; that is, the character is not greater than 26. If it is, then subtract 26, like this
newPosition = characterPosition + key
if newPosition >= len(lettersList) then:
newPosition -= len(lettersList)
or use the modulo operator as suggested. This should get you the idea how to modify the decrypt function to "wrap around" as well.