英文:
how i can print the result in one line and with space for each character
问题
input = hello
output = 01101000 01100101 01101100 01101100 01101111
英文:
binary = {
"a": "01100001",
"b": "01100010",
"c": "01100011",
"d": "01100100",
"e": "01100101",
"f": "01100110",
"g": "01100111",
"h": "01101000",
"i": "01101001",
"j": "01101010",
"k": "01101011",
"l": "01101100",
"m": "01101101",
"n": "01101110",
"o": "01101111",
"p": "01110000",
"q": "01110001",
"r": "01110010",
"s": "01110011",
"t": "01110100",
"u": "01110101",
"v": "01110110",
"w": "01110111",
"x": "01111000",
"y": "01111001",
"z": "01111010"
}
while True:
converter = input("Convert decimal to binary: ")
for i in converter:
print(binary.get(i))
i was expecting the output be like this:
ex:
input = hello
output = 01101000 01100101 01101100 01101100 01101111
答案1
得分: 1
你可以通过提供end
参数来控制在打印字符串末尾添加的内容。
while True:
converter = input("将十进制转换为二进制:")
for i in converter:
print(binary.get(i), end=" ") # 打印二进制数后添加一个空格
print() # 这会打印一个新行
英文:
You could control what is added at the end of a printed string, by providing the end
parameter.
while True:
converter = input("Convert decimal to binary: ")
for i in converter:
print(binary.get(i), end=" ") # add a space after the printed binary number
print() # this prints a new line
答案2
得分: 0
你可以使用 sep
参数来指定在将输入字符映射到 binary.get
方法时,用于 print
函数的分隔符:
while True:
converter = input("将十进制转换为二进制:")
print(*map(binary.get, converter), sep=' ')
英文:
You can use the sep
argument to specify a separator for the print
function while mapping characters from the input to the binary.get
method:
while True:
converter = input("Convert decimal to binary: ")
print(*map(binary.get, converter), sep=' ')
答案3
得分: 0
binary = { "a": "01100001", "b": "01100010", "c": "01100011", "d": "01100100", "e": "01100101", "f": "01100110", "g": "01100111", "h": "01101000", "i": "01101001", "j": "01101010", "k": "01101011", "l": "01101100", "m": "01101101", "n": "01101110", "o": "01101111", "p": "01110000", "q": "01110001", "r": "01110010", "s": "01110011", "t": "01110100", "u": "01110101", "v": "01110110", "w": "01110111", "x": "01111000", "y": "01111001", "z": "01111010" }
word = []
while True:
converter = input("Convert decimal to binary: ")
for i in converter:
word.append(binary.get(i))
print(word)
word = []
英文:
binary = { "a": "01100001", "b": "01100010", "c": "01100011", "d": "01100100", "e": "01100101", "f": "01100110", "g": "01100111", "h": "01101000", "i": "01101001", "j": "01101010", "k": "01101011", "l": "01101100", "m": "01101101", "n": "01101110", "o": "01101111", "p": "01110000", "q": "01110001", "r": "01110010", "s": "01110011", "t": "01110100", "u": "01110101", "v": "01110110", "w": "01110111", "x": "01111000", "y": "01111001", "z": "01111010" }
word = []
while True:
converter = input("Convert decimal to binary: ")
for i in converter:
word.append(binary.get(i))
print(word)
word = []
This would print your word in a list and clear the list every time you put in a new word.
答案4
得分: 0
print(binary.get(i), end=" ") # 将end参数设置为一个空格间隔
英文:
your code is correct you just have to set the end parameter to " " in your print statement, as by default it is set to \n
print(binary.get(i), end=" ") #sets end parameter to a " "space gap
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论