如何在一行中打印结果,并为每个字符留出空格。

huangapple go评论101阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月26日 08:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定