如何从用户那里获取输入以在字典中找到键并输出其值?

San*_*age 2 python dictionary python-3.x

我正在用python创建一个简单的S-Box,它包含所有可能的3位组合作为密钥,并包含其加密组合作为其值。

它基本上会从用户那里获取3位,然后将其针对我定义的S-Box表运行,然后它将找到与用户输入位匹配的密钥并输出其加密值

下面的示例代码,不是完整的代码;

SBox= { "000": "110","001": "010","010":"000","011": "100" }

inputBits= input("Enter 3 bit pattern: ")

if inputBits == "000":
        print("Encrypted combo: ", SBox["000"])
Run Code Online (Sandbox Code Playgroud)

输出:

Enter 3 bit pattern: 000
Encrypted combo: 110
Run Code Online (Sandbox Code Playgroud)

我希望能够更有效地执行此操作,即:不必为每个可能的组合都使用if,它类似于将输入字符串与双数字键匹配。

任何帮助表示赞赏!

Rak*_*esh 5

使用 dict.get

例如:

SBox= { "000": "110","001": "010","010":"000","011": "100" }

inputBits= input("Enter 3 bit pattern: ")

if SBox.get(inputBits):
    print("Encrypted combo: ", SBox.get(inputBits))

#OR print("Encrypted combo: ", SBox.get(inputBits, "N\A"))
Run Code Online (Sandbox Code Playgroud)