最終,我將能夠在聊天室中發佈類似這樣的簡單問題,但現在我必須發佈它。我仍在努力解決Python中的比較問題。我有一個包含從文件中獲得的字符串的列表。我有一個函數,它包含單詞列表(以前由文件創建)和一些「密文」。我正在嘗試使用Shift Cipher破解密文。我的問題與比較整數相同。雖然我可以看到在嘗試使用打印語句進行調試時,我的密文將被轉移到單詞列表中的單詞,但它永遠不會評估爲True。我可能會比較兩種不同的變量類型,或者一個/ n可能會將比較關閉。對不起今天的所有帖子,我今天在做準備即將到來的作業方面做了很多練習題。Python字符串比較使用單詞列表
def shift_encrypt(s, m):
shiftAmt = s % 26
msgAsNumList = string2nlist(m)
shiftedNumList = add_val_mod26(msgAsNumList, shiftAmt)
print 'Here is the shifted number list: ', shiftedNumList
# Take the shifted number list and convert it back to a string
numListtoMsg = nlist2string(shiftedNumList)
msgString = ''.join(numListtoMsg)
return msgString
def add_val_mod26(nlist, value):
newValue = value % 26
print 'Value to Add after mod 26: ', newValue
listLen = len(nlist)
index = 0
while index < listLen:
nlist[index] = (nlist[index] + newValue) % 26
index = index + 1
return nlist
def string2nlist(m):
characters = ['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']
numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
newList = []
msgLen = len(m) # var msgLen will be an integer of the length
index = 0 # iterate through message length in while loop
while index < msgLen:
letter = m[index] # iterate through message m
i = 0
while i < 26:
if letter == characters[i]:
newList.append(numbers[i])
i = i + 1
index = index + 1
return newList
def nlist2string(nlist):
characters = ['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']
numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
newList = []
nListLen = len(nlist)
index = 0
while index < nListLen:
num = nlist[index]
newNum = num % 26
i = 0
while i < 26:
num1 = newNum
num2 = numbers[i]
if (num1 == num2):
newList.append(characters[i])
i = i + 1
index = index + 1
return newList
def wordList(filename):
fileObject = open(filename, "r+")
wordsList = fileObject.readlines()
return wordsList
def shift_computePlaintext(wlist, c):
index = 0
while index < 26:
newCipher = shift_encrypt(index, c)
print 'The new cipher text is: ', newCipher
wordlistLen = len(wlist)
i = 0
while i < wordlistLen:
print wlist[i]
if newCipher == wlist[i]:
return newCipher
else:
print 'Word not found.'
i = i + 1
index = index + 1
print 'Take Ciphertext and Find Plaintext from Wordlist Function: \n'
list = wordList('test.txt')
print list
plainText = shift_computePlaintext(list, 'vium')
print 'The plaintext was found in the wordlist: ', plainText
當偏移量= 18,密文=名稱這是我的單詞表一個詞,但它永遠不會計算爲True。感謝您提前提供任何幫助!
嘗試打印'repr(x)'而不是'x'。這樣可以更容易看到額外的''\ n''或其他空白字符,並區分'2'和'2',以及所有其他您正在討論的內容。 – abarnert
同時,你可以給我們'test.txt'的內容,並修正縮進,幷包括一個完整的示例(所有這些函數'shift_encrypt'調用),所以我們可以真正運行你的代碼並進行調試嗎? – abarnert
作爲一個方面說明,調用一個變量'list'是個壞主意,因爲這是內建'list'類型的名稱,您將無法再訪問它。 – abarnert