我需要帮助来写这个程序的第二部分,它几乎完成了。我完成了字母表加密的部分,但现在我发现自己被困在尝试使用新的加密作为一种解码器环,来解密一个随机的被置乱的消息,有人可能会输入到程序中。在
为了让这更容易理解,让我们假设这个程序的加密部分提供给您:NHPWJXEYOZMAFUSCIGLVTDBKRQ
现在有人会输入一些随机的信息,使用新的加密字母表加密。假设有人输入:VYOL OL FR 1LV LJPGJV FJLLNEJ!
现在,我需要写的程序将不得不使用加扰字母表解码消息并打印:这是我的第一条秘密消息!
如果有人能帮忙,我会很感激的。如果听起来还是让人困惑,那就问吧。我的程序如下:
# ENCODE a secret message
# Scramble the alphabet, read a secret message, encode it, print scrambled
import random
def main():
encryption()
decryption()
def encryption():
encrypt=["*"]*26 # all letters available
print(encrypt)
print("Alphabet: ", end="")
for numbah in range(26):
#converts numbah into a letter
letter = chr(numbah+65) # converts 0-25 --> 'A' = 'Z'
print(letter, end="")
#Reminder: find an empty position for that letter to be placed
notfound = True
while notfound:
possible_position = random.randint(0,25)
if encrypt[possible_position] == "*":
notfound = False
encrypt[possible_position] = letter
print("\nScrambled: " , end="")
for numbah in range(26):
print(encrypt[numbah], end="")
print("\n\n")
msg=input("Now, please type your secret message to encode: ")
print("Your secret message: " + msg)
print("Your message encoded: ", end="")
# reminder non alphabetic characters should 'float thru' unchanged!
for alpha in msg.upper():
if alpha < "A" or alpha > "Z":
print(alpha, end="")
else:
print( encrypt[ ord(alpha) - 65], end="")
print("\n")
def decryption():
scram_alph = input("Input the scrambled alphabet from the early prog: ")
scram_mess = input("Input the scrambled messgae you want decoded: ")
main()
Python标准库中有一些函数使这项任务非常简单。看看^{} 和{a2}:
你太复杂了。使用Python提供的内置迭代工具。在
解释
线
^{pr2}$将密文字母转换成明文字母。为什么?^两个字符串的{}给你一个成对的列表:(第一个字母,第一个字母),(第二个字母,第二个字母),等等,然后{}组成一个字典。然后排队
表示在字典中查找消息中的每个字符(如果没有,则为空格或a!然后不要改变它)和胶水他们回到一个字符串。在
顺便说一下,做一个拼凑的字母表你需要做的就是
相关问题 更多 >
编程相关推荐