不知道為什么我的代碼沒有正確加密密碼例如:“你好,你好嗎”是“2z669 29g vbz i9e”,我的代碼會很好地解密它。如果我嘗試加密它,我會得到:“e]z } g vbz I e”。我假設它與 21 模數 len(ALPHABET) 有關,我只是想如果解密是減去我需??要添加以反轉密碼,但我想我錯了任何幫助將不勝感激謝謝!
ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789."
def encrpytstr(message):
newString = ''
#loop to assign new alphabet value
for index in range(0, len(message)):
oneLetter = message[index]
#assinging new alphabet value
newAlphabetValue = (ALPHABET.find(oneLetter.lower()) 21) % len(ALPHABET) (ord('A') if oneLetter.isupper() else ord('a'))
#checking for white spaces
if oneLetter == ' ':
newAlphabetValue = 32
#creating the new string
newString = newString chr(newAlphabetValue)
return newString
def decrpytstr(message):
newString = ''
#loop to assign new alphabet value
for index in range(0, len(message)):
oneLetter = message[index]
#assinging new alphabet value
newAlphabetValue = (ALPHABET.find(oneLetter.lower()) - 21) % len(ALPHABET) (ord('A') if oneLetter.isupper() else ord('a'))
#checking for white spaces
if oneLetter == ' ':
newAlphabetValue = 32
#creating the new string
newString = newString chr(newAlphabetValue)
return newString
def main():
message = input("Enter text: ")
choice = input("(E)ncrypt or (D)ecrypt?: ").upper()
if choice == "E":
encrpytstr(message)
newString = encrpytstr(message)
print(newString)
input("Press ENTER to exit")
elif choice == "D":
decrpytstr(message)
newString = decrpytstr(message)
print(newString)
input("Press ENTER to exit")
else:
print("Invalid Input Try Again!")
input("Press ENTER to exit")
main()
uj5u.com熱心網友回復:
添加幾個列印陳述句揭示了您的邏輯的一個相當基本的問題。
#assinging new alphabet value
newAlphabetValue = (ALPHABET.find(oneLetter.lower()) 21) % len(ALPHABET) (ord('A') if oneLetter.isupper() else ord('a'))
print("input: %s new: %s" % (oneLetter, chr(newAlphabetValue)))
print("(found %i 21 %i)" % (ALPHABET.find(oneLetter.lower()), ALPHABET.find(oneLetter.lower()) 21))
生產
input: H new: ]
(found 7 21 28)
input: e new: z
(found 4 21 25)
input: l new:
(found 11 21 32)
input: l new:
(found 11 21 32)
input: o new: ?
(found 14 21 35)
input: , new: u
(found -1 21 20)
input: new: u
(found -1 21 20)
input: h new: }
(found 7 21 28)
input: o new: ?
(found 14 21 35)
input: w new: g
(found 22 21 43)
input: new: u
(found -1 21 20)
input: a new: v
(found 0 21 21)
input: r new: b
(found 17 21 38)
input: e new: z
(found 4 21 25)
input: new: u
(found -1 21 20)
input: y new: i
(found 24 21 45)
input: o new: ?
(found 14 21 35)
input: u new: e
(found 20 21 41)
input: ? new: u
(found -1 21 20)
注意結果find是如何經常是-1,這又是如何把所有東西都扔掉的?
我不確定到底出了什么問題,因為不清楚你認為這應該如何作業。也許簡化代碼,以便更清楚發生了什么以及為什么。
繞道index也是完全沒有必要的;Python 完全能夠直接遍歷串列中的專案。順便說一句,如果可以的話,可能避免重復地將東西附加到字串的末尾。
def encrpytstr(message):
# Use a list for speed
newletters = []
for letter in message:
upcase = letter.isupper()
letter = letter.lower()
index = ALPHABET.find(letter)
if index > -1:
newindex = (index 21) % len(ALPHABET)
# print("input: %s (%i, %i) new: %s (%i, %i)" % (letter, index, ord(letter), ALPHABET[newindex], newindex, ord(ALPHABET[newindex])))
letter = ALPHABET[newindex]
if upcase:
letter = letter.upper()
newletters.append(letter)
# now finally do a slow string join
return ''.join(newletters)
print如果您想使用新代碼重復除錯步驟,我留下了一個注釋掉的陳述句。
新代碼只是逐字保留不在ALPHABET;中的任何字符。如果您不希望更改,應該很容易看到要更改的內容。
最后,你是不是故意拼錯了“encrpyt”和“decrpyt”?當你第一次看到它時,它有點幽默,但一旦你的代碼增長,它就會在后面咬你。避免函式名稱中出現奇怪的拼寫錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/415231.html
標籤:
上一篇:Julia代碼中的記憶體分配問題
