python - 如何从用户那里获取输入以在字典中查找键并输出其值?
问题描述
我在 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"])
输出:
Enter 3 bit pattern: 000
Encrypted combo: 110
我希望能够更有效地做到这一点,即:不必为每个可能的组合都有一个 if,类似于将输入字符串与字典中的键匹配的东西。
任何帮助表示赞赏!
解决方案
利用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"))
推荐阅读
- reactjs - 在 Windows Server 2008 R2 上的 IIS 上发布的 React 应用程序显示空白页
- mysql - 尝试执行更新查询,其中行是来自 TEdit 的变量
- r - 对导入的 .xlsx 数据集运行线性回归,将列的每一行作为系数返回,而不仅仅是列名
- swift - AVPlayer 字幕选择屏幕的完成按钮始终为白色
- firebase - 如何在颤动中赋予屏幕滚动效果颜色
- vue.js - 从 Vue 函数中设置选定的 ion-select-option Ionic
- c# - 插入表时更改的值
- javascript - 是否可以将 graphql 提取结果格式化为可读列表?
- python-3.x - Ubuntu上python中“导入rarfile”的问题
- flutter - Flutter:有条件地显示不同的小部件