基本上我有一個函數,將非常簡單地加密一條消息。找到簡單的方法來翻譯基於密鑰的字符串
def encrypt(message):
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"]
key = ["4","x","z","@","%","b","j","q","(","ƒ","¥","µ","˚","nå","ø","π","å","œ","¢","∞","∫","µ","≈","`","¬","…"]
new_message = ""
for x in range(0,len(message)):
new_message = message.replace(message[x],key.index[alphabet.index(message[x])])
return new_message
print(encrypt(input("What would you like to encrypt").lower()))
這應該採信,並在列表鍵相同的索引字符替換它,但我得到的錯誤:
TypeError: 'builtin_function_or_method' object is not subscriptable
有這樣做的更好的方法,但你的問題是'key.index [alphabet.index(message [x])]''。改爲使用'鍵[alphabet.index(message [x])]'。 –
@JaredGoguen我不認爲這會起作用,一些鑰匙包含在字母表中。這是'str.translate'的一個用例。 –
@ juanpa.arrivillaga哈哈,同意......直到我發佈我的答案後纔看到此消息。 –