英文:
it only writes one letter and exites the loop
问题
以下是您的翻译内容:
我制作了一个简单的“加密”代码,将字符串中的每个字母转换为特定的字母,例如
input = CS
“C => 3X, S => 19G”
Output = 3X19G
加密正常工作,但解密代码有问题,当我运行它时,它只打印第一个字母然后退出循环或类似的操作
def decrypt(string):
decrypted = ""
decrypt_letter = ""
for letter in string :
decrypt_letter += letter
if decrypt_letter in encryption.values() :
decrypted +="".join(list(encryption.keys())[list(encryption.values()).index(decrypt_letter)])
decrypt_letter = ""
print(decrypted)
input = 3X19G
output = "C"
当然忽略"S"
encryption = {"A":"1Z","B":"2Y","C":"3X","D":"4W","E":"5V","F":"6T","G":"7S","H":"8R","I":"9Q","J":"10P","K":"11O","L":"12N",
"M":"13","N":"14L","O":"15K","P":"16J","Q":"17I","R":"18H","S":"19G","T":"20F","U":"21E","V":"22D","W":"23C","X":"24B","Y":"25A",
"Z":"26","a":"1z","b":"2y","c":"3x","d":"w","e":"5v","f":"6t","g":"7s","h":"8r","i":"9q","j":"10p","k":"11o","l":"12n","m":"13s",
"n":"14l","o":"15k","p":"16j","q":"17i","r":"18h","s":"19g","t":"20f","u":"21e","v":"22d","w":"23c","x":"24b","y":"25a","z":"26"," ":"\s"}
英文:
i made a simple "Encrypting" code that turns every letter in a string to a specific letter, like
input = CS
"C => 3X, S => 19G"
Output = 3X19G
the encryption works fine, but the problem is with the decryption code below, when i run it, it only prints the first letter then exits the loop or something
def decrypt(string):
decrypted = ""
decrypt_letter = ""
for letter in string :
decrypt_letter += letter
if decrypt_letter in encryption.values() :
decrypted +="".join(list(encryption.keys())[list(encryption.values()).index(decrypt_letter)])
decrypt_letter = ""
print(decrypted)
input = 3X19G
output = "C"
and ignoring the "S" of course
encryption = {"A":"1Z","B":"2Y","C":"3X","D":"4W","E":"5V","F":"6T","G":"7S","H":"8R","I":"9Q","J":"10P","K":"11O","L":"12N",
"M":"13","N":"14L","O":"15K","P":"16J","Q":"17I","R":"18H","S":"19G","T":"20F","U":"21E","V":"22D","W":"23C","X":"24B","Y":"25A",
"Z":"26","a":"1z","b":"2y","c":"3x","d":"w","e":"5v","f":"6t","g":"7s","h":"8r","i":"9q","j":"10p","k":"11o","l":"12n","m":"13s",
"n":"14l","o":"15k","p":"16j","q":"17i","r":"18h","s":"19g","t":"20f","u":"21e","v":"22d","w":"23c","x":"24b","y":"25a","z":"26"," ":'\s'}
this is the encryption/decryption list
sorry for the code uglieness
答案1
得分: 1
你需要在解密一个字母后,在if块内重置decrypt_letter
,并遍历加密字符串的字符,而不仅仅是字母。
def decrypt(string):
decrypted = ""
decrypt_letter = ""
for char in string:
decrypt_letter += char
if decrypt_letter in encryption.values():
decrypted += "".join(list(encryption.keys())[list(encryption.values()).index(decrypt_letter)])
decrypt_letter = ""
print(decrypted)
英文:
You need to reset decrypt_letter
inside the if block after decrypting a letter and iterate over the characters of the encrypted string, not just the letters.
def decrypt(string):
decrypted = ""
decrypt_letter = ""
for char in string:
decrypt_letter += char
if decrypt_letter in encryption.values():
decrypted += "".join(list(encryption.keys())[list(encryption.values()).index(decrypt_letter)])
decrypt_letter = ""
print(decrypted)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论