只写一个字母并退出循环。

huangapple go评论65阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年6月8日 08:14:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定