继网友的评论,使用字典这种情况下编程的一个更好的做法,你就只需要填写字典letterToBin
,你可以在例如
这是一本词典看到,至极意味着它将有钥匙,和值,像手机,你有钥匙的名字(你的母亲)和价值(他的手机):
letterToBin = {}
letterToBin = {
"a" : "01000001", #Here, the key is the "a" letter, and the value, his bin transformation, the 01000001
"b" : "01000010",
"c" : "01000011",
"d" : "01000100"
#so you need to add all the other keys you need, for example the "e"
"e" : "01000101" #for example
}
binToLetter = {} # here I create a second dictionary, and it invert the values of the first, it meas, now the keys will be the bins, and the value the latters
binToLetter = dict(zip(letterToBin.values(), letterToBin.keys())) #this code do the magic, you must understand, that only needs to feel the first dictionary, and for free, you will have the second dictionary
wordOrBin = input("enter here: ")
if wordOrBin in letterToBin:
print(letterToBin[wordOrBin]) #here I has if you write a latter (a) or a bin(11001101) and it choose where to look the correct value
else:
print(binToLetter[wordOrBin])
您正在打印的是您输入的“单词” – VK321
您没有描述您的困难,也没有提出问题,这是一个问答网站,因此您的文本中某处的问号有助于找到您实际上是在问(例如,如果我切换到使用Java,这个神奇的工作,而不编码?) – Anthon
如果我理解的很好,她想打印变量的值,将输入与变量的名称进行匹配,所以如果输入“a”字符,则不会得到结果,但是01000001,在我的答案中我认为它的工作原理 –