3

My task is to create a program that will decode a caesar cypher. I have done most of the work for this and it works fine going through each possible key and finding the new word.

The problem appears when I am trying to check the words that are created.

#---------------------------------
#this is a program that should brute force a ceaser cypher
#---------------------------------
#list of possible letters
x=['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']
y=input("Please input the string you want to decode:   ").lower()
new_string=''
#creates a list to check weather the unjumbled word is valid
AllowedWords = []
WordsFile = open("aqawords.txt", "r")
for Word in WordsFile:
  AllowedWords.append(Word.strip().lower())
WordsFile.close()
#does this 26 times to test all possible keys
for f in range (1,27):
    #does each letter individually
    for i in range(len(y)):
        #finds the numerical value for each leter
        y1=(x.index((y[i:(i+1)])))+1
        #adds the key to the letter's number
        y2=y1+f
        #makes sure it does not go out of range
        y2=y2%26
        #adds new letter to string
        new_string=new_string+str(x[y2-1])
        #this should check every different combination of letters
        for q in range(1,(len(new_string))+1):
            for q1 in range(1,(len(new_string))+1):
                #here is the problem-----------
                #checks what type the variables are
                print(type(q))
                print(type(q1))
                #attempts to create a new string to store these new words
                new_string_1=new_string[q,q1]
                #says "TypeError: string indices must be integers"
                #but i checked and above it says that they are ints
                if new_string_1 in AllowedWords:
                    print(new_string_1)
                #------------------------------
                #print(new_string_1)
        new_string_1=''
    #print(new_string)
    new_string=''

This is what I get as a result:

RESTART: D:\!!computer science!!\!homework!\miss jackson's homework\cyphers\Ceasar cypher.py 
Please input the string you want to decode:   abc
<class 'int'>
<class 'int'>
Traceback (most recent call last):
  File "D:\!!computer science!!\!homework!\miss jackson's homework\cyphers\Ceasar cypher.py", line 35, in <module>
    new_string_1=new_string[q,q1]
TypeError: string indices must be integers
>>> 
 RESTART: D:\!!computer science!!\!homework!\miss jackson's homework\cyphers\Ceasar cypher.py 
Please input the string you want to decode:   a
<class 'int'>
<class 'int'>
Traceback (most recent call last):
  File "D:\!!computer science!!\!homework!\miss jackson's homework\cyphers\Ceasar cypher.py", line 35, in <module>
    new_string_1=new_string[q,q1]
TypeError: string indices must be integers

Can anyone find where I went wrong?

David Makogon
  • 69,407
  • 21
  • 141
  • 189
aidan
  • 85
  • 1
  • 2
  • 8
  • 5
    What is `new_string[q,q1]` supposed to do? What, for example, should `"hello"[q, q1]` give you? – timgeb Dec 14 '17 at 11:02
  • 1
    I think you intend a colon between `q` and `q1` rather than a comma. – John Coleman Dec 14 '17 at 11:41
  • Thank you for the answer it fixed the typeerror i was getting, but I still don't have a clue about how to check weather the words have been decoded. I can check to see if they're in the list of allowed words, but doesn't work for sentences, how can I check every possible word formed to create the correct sentence? – aidan Dec 15 '17 at 03:15
  • @aidan That is literally a different question, and you would have more luck asking it as such then trying to get this question reopened. – John Coleman Dec 15 '17 at 15:15

0 Answers0