我在做python中的加密分配,我需要: - 分割字符串 - 替換字母 - 重新加入它,以便它是一個單詞。python如何分割字符串並重新加入
這是完整的代碼,但我卡在def encode(plain)
下的for循環。
""" crypto.py
Implements a simple substitution cypher
"""
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key = "XPMGTDHLYONZBWEARKJUFSCIQV"
def menu():
print("Secret decoder menu")
print("0) Quit")
print("1) Encode")
print("2) Decode")
print("What do you want to do?")
response = input()
return response
def encode(plain):
for i in range(len(plain)):
plain = plain.upper()
x = plain[i:i+1]
y = alpha.index(x)
z = key[y:y+1]
plain[x] = z
return plain
def main():
keepGoing = True
while keepGoing:
response = menu()
if response == "1":
plain = input("text to be encoded: ")
print(encode(plain))
elif response == "2":
coded = input("code to be decyphered: ")
print (decode(coded))
elif response == "0":
print ("Thanks for doing secret spy stuff with me.")
keepGoing = False
else:
print ("I don't know what you want to do...")
return main
main()
menu()
樣品的輸入和輸出預計將是很好的。 –
你需要在你的函數定義下縮進代碼 – 0TTT0
解密器菜單的祕密 0)退出 1)編碼 2)解碼 你想做什麼? 文本進行編碼:你好 大號 牛逼 ž ž Ë HELLO – Matteo