I am currently programming the Caesar Cipher.
- I created a list of the alphabets
- It simply asks for a sentence
- Gets the index position of each letter in the sentence in corresponding to the alphabet list,
- Adds the offset onto each offset using a while loop, creating a new index
- Prints out the corresponding alphabet list index with new index which makes a coded word.
- Therefore creating a coded sentence
The problem is that the alphabet list does not contain spaces, so I get an error when I try to make a sentence (because it is separated by spaces), only single words/letters work...
CODE HERE:
#Creating Lists
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"]
#Unpickling Dictionary
unpickle_codedwords = open("CWs.txt", "rb")
codedwords = pickle.load(unpickle_codedwords)
unpickle_codedwords.close()
###############################################################
""" #
IMPROVMENTS: #
Spaces/Sentences dont work <-- bob is a boy (ERROR) #
""" #
###############################################################
loop = 0#Using this to make my program loop continously
while loop == 0:
choice=int(input("What would you like to do?\n1)Code a word\n2)Decode a word\n3)Print the coded words list\n4)Quit the program\n5)Clear Coded Words List\n>>>"))#Asking for choice from user
if choice ==1:#If the choice selected was 1:
word=input("Enter the word you want to code\n>>>")
offset=int(input("Enter the offset below\n>>>"))
if word.isalpha() ==True:#checking alphabet only
for letter in word.lower(): # getting index of everysingle letter and converting it to lowercase
index=alphabet.index(letter)
index=index+offset#adding the index to the offset to create new index
while index>25:#if index is more than 25 i have to while loop it
index=index-26#creatingn the new index
codedwords.append([alphabet[index]])#appending each letter to word
#print(alphabet[index])<-- REMOVE LATER
answer = [''.join([x[0] for x in codedwords])] #instead of print(codedwords) because that prints[i],[i],[i] not iii
print(answer)
while word.isalpha()==False:#if word is not alphabeticals
print("Invalid Entry!, Please Try again\n")#loop it around again untill it's correct
word=input("Enter the word you want to code\n>>>")
if word.isalpha()==True:#looping round untill correct
for letter in word.lower():
index=alphabet.index(letter)
index=index+offset#Repeated again as above
while index>25:
index=index-26
codedwords.append([alphabet[index]])
answer = [''.join([x[0] for x in codedwords])]
print(answer)