我的任务是创建一个解码凯撒密码的程序。 我为此完成了大部分工作,它可以很好地完成每个可能的键并找到新单词。
当我尝试检查创建的单词时,会出现问题。
#---------------------------------
#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=''
这就是我得到的结果:
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
有人能找到我错的地方吗?