Python 凯撒密码,大写字母
因此,目前我的凯撒密码程序运行良好,每当我使用小写字母。但是,当我输入大写的单词或短语时,我希望它能够工作。这就是我现在的代码。希望你们能帮我完成这件事 用户定义函数 def加密(消息、距离): “”“将接收邮件并将其旋转一定距离,以便创建加密邮件”“” def解密(消息、距离): “”“将解密上述消息”“” while循环 运行=真 运行时:Python 凯撒密码,大写字母,python,caesar-cipher,Python,Caesar Cipher,因此,目前我的凯撒密码程序运行良好,每当我使用小写字母。但是,当我输入大写的单词或短语时,我希望它能够工作。这就是我现在的代码。希望你们能帮我完成这件事 用户定义函数 def加密(消息、距离): “”“将接收邮件并将其旋转一定距离,以便创建加密邮件”“” def解密(消息、距离): “”“将解密上述消息”“” while循环 运行=真 运行时: #input message = input("Enter word to be encrypted: ") #original message d
#input
message = input("Enter word to be encrypted: ") #original message
distance = int(input("Enter the distance value: ")) #distance letters will be moved
#variables
fancy = encrypt(message, distance)
boring = decrypt(fancy, distance)
numbers = binaryConversion(message)
#output
print("\n")
print("Your word was: ", format(message, ">20s"))
print("The distance you rotated was: ", format(distance), "\n")
print("The encryption is: ", format(fancy, ">16s"))
print("The decryption is: ", format(boring, ">16s"))
print("The binary code is: ", format(numbers)) #I know an error comes here but it will work in the end
repeat = input("Would you like to encrypt again? Y/N ")
print("\n")
if repeat == "N" or repeat == "n":
run = False
else:
run = True
结局
印刷品(“谢谢你&正如朱利叶斯·凯撒曾经说过的,‘威尼斯、维迪、维西’”)
谢谢我建议您以映射而不是偏移的心态来处理这个问题。可以基于偏移量构建映射,但如果使用字典或其他形式的一对一映射,字符处理将更容易 例如:
offset = 5
source = "abcdefghijklmnopqrstuvwxyz"
target = source[offset:]+source[:offset]
source = source + source.upper()
target = target + target.upper()
encrypt = str.maketrans(source,target)
decrypt = str.maketrans(target,source)
e = "The quick brown Fox jumped over the lazy Dogs".translate(encrypt)
print(e)
d = e.translate(decrypt)
print(d)
我想我错过了。我试过了,但我也需要用大写字母从解密中打印出来
binary = ""
for cb in message:
binaryString = " " #Binary number
binaryNumber = ord(cb)
while binaryNumber > 0:
binaryRemainder = binaryNumber % 2
binaryNumber = binaryNumber // 2
binaryString = str(binaryRemainder) + binaryString
binary += binaryString
return binary
#input
message = input("Enter word to be encrypted: ") #original message
distance = int(input("Enter the distance value: ")) #distance letters will be moved
#variables
fancy = encrypt(message, distance)
boring = decrypt(fancy, distance)
numbers = binaryConversion(message)
#output
print("\n")
print("Your word was: ", format(message, ">20s"))
print("The distance you rotated was: ", format(distance), "\n")
print("The encryption is: ", format(fancy, ">16s"))
print("The decryption is: ", format(boring, ">16s"))
print("The binary code is: ", format(numbers)) #I know an error comes here but it will work in the end
repeat = input("Would you like to encrypt again? Y/N ")
print("\n")
if repeat == "N" or repeat == "n":
run = False
else:
run = True
offset = 5
source = "abcdefghijklmnopqrstuvwxyz"
target = source[offset:]+source[:offset]
source = source + source.upper()
target = target + target.upper()
encrypt = str.maketrans(source,target)
decrypt = str.maketrans(target,source)
e = "The quick brown Fox jumped over the lazy Dogs".translate(encrypt)
print(e)
d = e.translate(decrypt)
print(d)