Python 3.x Python 3:无法搜索超过1个字符的字典键

Python 3.x Python 3:无法搜索超过1个字符的字典键,python-3.x,dictionary,Python 3.x,Dictionary,我对Python非常陌生,而且才刚刚开始学习,所以任何建议和提示都会非常棒。 我正在尝试制作一个程序,允许用户将文本转换成二进制 这个过程是文本>十六进制>二进制 我这样做主要是为了挑战自己,更好地理解Python的工作原理,所以请原谅这些糟糕的代码 im面临的问题是,当用户输入十六进制值(如“41”)时,程序应检查字典中的关联值,即“01000001”。但是,我得到了以下错误消息: Traceback (most recent call last): File "c:\Users\K

我对Python非常陌生,而且才刚刚开始学习,所以任何建议和提示都会非常棒。 我正在尝试制作一个程序,允许用户将文本转换成二进制

这个过程是文本>十六进制>二进制

我这样做主要是为了挑战自己,更好地理解Python的工作原理,所以请原谅这些糟糕的代码

im面临的问题是,当用户输入十六进制值(如“41”)时,程序应检查字典中的关联值,即“01000001”。但是,我得到了以下错误消息:

Traceback (most recent call last):
File "c:\Users\Kye\Desktop\Python\Word Encrypter.py\Word Encryption.py", line 48, in <module>
print(to_bin_code())
File "c:\Users\Kye\Desktop\Python\Word Encrypter.py\Word Encryption.py", line 44, in to_bin_code
bin_code = bin_code + hex_to_bin_dic[user_text]
KeyError: '2'
这是支持将十六进制转换为二进制的块。现在的钥匙只是为了显示我的问题,唯一留下来的钥匙是“42”。但是,如果运行程序键“A”和“4”可以工作,但键“42”和“CC”不工作

这是完整的代码。此外,我知道很多可以缩写,但我现在希望它是最基本的形式,以便我能更好地理解它:

print("Would you like to do a Hexidecimal Conversion or Binary Conversion? ")

hex_or_bin = input()

if hex_or_bin == ("Hex" or "Hexidecimal"):
    def to_hex_code():
        word_to_hex_dic = {
            "A": "41", "B": "42", "C": "43", "D": "44", "E": "45",
            "F": "46", "G": "47", "H": "48", "I": "49", "J": "4A", "K": "4B", "L": "4C", "M": "4D", "N": "4E",
            "O": "4F", "P": "50", "Q": "51", "R": "52", "S": "53", "T": "54", "U": "55", "V": "56", "W": "57",
            "X": "58", "Y": "59", "Z": "5A", "a": "61", "b": "62", "c": "63", "d": "64", "e": "65", "f": "66",
            "g": "67", "h": "68", "i": "69", "j": "6A", "k": "6B", "l": "6C", "m": "6D", "n": "6E", "o": "6F",
            "p": "70", "q": "71", "r": "72", "s": "73", "t": "74", "u": "75", "v": "76", "w": "77", "x": "78",
            "y": "79", "z": "7A", " ": "20", "!": "21", "\"": "22", "#": "23", "$": "24", "%": "25", "&": "26",
            "'": "27", "(": "28", ")": "29", "*": "2A", "+": "2B", ",": "2C", "-": "2D", ".": "2E", "/": "2F",
            ":": "3A", ";": "3B", "<": "3C", "=": "3D", ">": "3E", "?": "3F", "@": "30", "[": "5B", "\\": "5C",
            "]": "5D", "^": "5E", "_": "5F", "`": "60", "{": "7B", "|": "7C", "}": "7D", "~": "7E", "0": "30",
            "1": "31", "2": "32", "3": "33", "4": "34", "5": "35", "6": "36", "7": "37", "8": "38", "9": "39",
}

#       hex_to_word_dic = {value: key for key,value in word_to_hex_dic.items()}

        hex_code = ""

        for user_text in text:
            hex_code = hex_code + word_to_hex_dic[user_text]
        return hex_code

    text=input("Type or Paste you message here for Hexidecimal conversion: \n")
    print(to_hex_code())

    to_hex_code()

elif hex_or_bin == ("Bin" or "Binary"):
    def to_bin_code():
        hex_to_bin_dic = {
            "A": "01000001", "42": "01000010", "CC": "01000011", "4": "01000100",
        }

        bin_code = ""

        for user_text in text:
            bin_code = bin_code + hex_to_bin_dic[user_text]
        return bin_code

    text=input("Type or Paste you Hexidecimal code here for conversion: \n")
    print(to_bin_code())

    to_bin_code()

else:
    print("Thankyou for using this converter")
print(“您想进行十六进制转换还是二进制转换?”)
hex_或_bin=输入()
如果十六进制或十六进制==(“十六进制”或“十六进制”):
def到_十六进制代码():
字到十六进制dic={
“A:”41“,”B:”42“,”C:”43“,”D:”44“,”E:”45“,
“F”:“46”、“G”:“47”、“H”:“48”、“I”:“49”、“J”:“4A”、“K”:“4B”、“L”:“4C”、“M”:“4D”、“N”:“4E”,
“O”:“4F”,“P”:“50”,“Q”:“51”,“R”:“52”,“S”:“53”,“T”:“54”,“U”:“55”,“V”:“56”,“W”:“57”,
“X”:“58”、“Y”:“59”、“Z”:“5A”、“a”:“61”、“b”:“62”、“c”:“63”、“d”:“64”、“e”:“65”、“f”:“66”,
“g”:“67”、“h”:“68”、“i”:“69”、“j”:“6A”、“k”:“6B”、“l”:“6C”、“m”:“6D”、“n”:“6E”、“o”:“6F”,
“p”:“70”,“q”:“71”,“r”:“72”,“s”:“73”,“t”:“74”,“u”:“75”,“v”:“76”,“w”:“77”,“x”:“78”,
“y:“79”,“z:“7A”,“20”,“21”,“22”,“23”,“24”,“25”,“26”,
“:”27“,”(“:”28“,”):”29“,”*“:”2A“,”+“:”2B“,”,“:”2C“,”-“:”2D“,”:“2E”,“/”:“2F”,
“:”:“3A”、“;”:“3B”、“:“3E”、“?”:“3F”、“@:“30”、“[”:“5B”、“\\”:“5C”,
“]”:“5D”、“^”:“5E”、“5F”、“`”:“60”、“{”:“7B”、“7C”、“}”:“7D”、“~”:“7E”、“0”:“30”,
"1": "31", "2": "32", "3": "33", "4": "34", "5": "35", "6": "36", "7": "37", "8": "38", "9": "39",
}
#hex_to_word_dic={value:key for key,word_to_hex_dic.items()中的值]
十六进制代码=“”
对于文本中的用户\文本:
十六进制代码=十六进制代码+单词到十六进制dic[用户文本]
返回十六进制代码
text=input(“在此处键入或粘贴消息以进行十六进制转换:\n”)
打印(到十六进制代码()
至十六进制代码()
elif hex_或_bin==(“bin”或“Binary”):
def至_bin_代码():
十六进制到二进制dic={
“A”:“01000001”、“42”:“01000010”、“CC”:“01000011”、“4”:“01000100”,
}
bin_code=“”
对于文本中的用户\文本:
bin_代码=bin_代码+十六进制到dic[用户文本]
返回bin_代码
text=input(“在此处键入或粘贴十六进制代码以进行转换:\n”)
打印(到\u bin\u code())
至_bin_code()
其他:
打印(“感谢您使用此转换器”)
您应该改用查找表。将dict更改为:

hex_to_bin_dic = {
    "0": "0000", "1": "0001", "2": "0010", "3": "0011", 
    "4": "0100", "5": "0101", "6": "0110", "7": "0111", 
    "8": "1000", "9": "1001", "A": "1010", "B": "1011", 
    "C": "1100", "D": "1101", "E": "1110", "F": "1111"
    }

对于用户\u text中的文本
将遍历您的输入,每次一个字符。fwiw,您可能对
int(…,16)
(将字符串解析为十六进制)、
hex(…)
(将整数转换为十六进制字符串)、
bin(…)
(将整数转换为字符串)和/r
:b
/
:x
字符串格式说明符(分别为二进制和十六进制)您可能想稍微简化一下,刚开始学习Python,所以我还不知道很多。这具体做什么?它会有什么帮助?
hex_to_bin_dic = {
    "0": "0000", "1": "0001", "2": "0010", "3": "0011", 
    "4": "0100", "5": "0101", "6": "0110", "7": "0111", 
    "8": "1000", "9": "1001", "A": "1010", "B": "1011", 
    "C": "1100", "D": "1101", "E": "1110", "F": "1111"
    }