Search code examples
pythonipythoncaesar-cipher

Caesar Cipher Spacing Error


I am currently programming the Caesar Cipher.

  1. I created a list of the alphabets
  2. It simply asks for a sentence
  3. Gets the index position of each letter in the sentence in corresponding to the alphabet list,
  4. Adds the offset onto each offset using a while loop, creating a new index
  5. Prints out the corresponding alphabet list index with new index which makes a coded word.
  6. 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)

Solution

  • You can use all to check if all letters in user input (word) are alpahanumeric or spaces:

    while all(True if letter in alphabet + [' '] else False for letter in word): 
    
    >>> all([True, True])
    True
    >>> all([True, False])
    False
    >>> 
    

    After that:

    for letter in word: 
        if letter == ' ':
            codedwords.append([letter])
        else:
            # Code the letter