繼網友的評論,使用字典這種情況下編程的一個更好的做法,你就只需要填寫字典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,在我的答案中我認爲它的工作原理 –