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

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

how i can print the result in one line and with space for each character

问题

input = hello
output = 01101000 01100101 01101100 01101100 01101111

英文:
  1. binary = {
  2. "a": "01100001",
  3. "b": "01100010",
  4. "c": "01100011",
  5. "d": "01100100",
  6. "e": "01100101",
  7. "f": "01100110",
  8. "g": "01100111",
  9. "h": "01101000",
  10. "i": "01101001",
  11. "j": "01101010",
  12. "k": "01101011",
  13. "l": "01101100",
  14. "m": "01101101",
  15. "n": "01101110",
  16. "o": "01101111",
  17. "p": "01110000",
  18. "q": "01110001",
  19. "r": "01110010",
  20. "s": "01110011",
  21. "t": "01110100",
  22. "u": "01110101",
  23. "v": "01110110",
  24. "w": "01110111",
  25. "x": "01111000",
  26. "y": "01111001",
  27. "z": "01111010"
  28. }
  29. while True:
  30. converter = input("Convert decimal to binary: ")
  31. for i in converter:
  32. print(binary.get(i))

i was expecting the output be like this:
ex:

  1. input = hello
  2. output = 01101000 01100101 01101100 01101100 01101111

答案1

得分: 1

你可以通过提供end参数来控制在打印字符串末尾添加的内容。

  1. while True:
  2. converter = input("将十进制转换为二进制:")
  3. for i in converter:
  4. print(binary.get(i), end=" ") # 打印二进制数后添加一个空格
  5. print() # 这会打印一个新行
英文:

You could control what is added at the end of a printed string, by providing the end parameter.

  1. while True:
  2. converter = input("Convert decimal to binary: ")
  3. for i in converter:
  4. print(binary.get(i), end=" ") # add a space after the printed binary number
  5. print() # this prints a new line

答案2

得分: 0

你可以使用 sep 参数来指定在将输入字符映射到 binary.get 方法时,用于 print 函数的分隔符:

  1. while True:
  2. converter = input("将十进制转换为二进制:")
  3. 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:

  1. while True:
  2. converter = input("Convert decimal to binary: ")
  3. print(*map(binary.get, converter), sep=' ')

答案3

得分: 0

  1. 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" }
  2. word = []
  3. while True:
  4. converter = input("Convert decimal to binary: ")
  5. for i in converter:
  6. word.append(binary.get(i))
  7. print(word)
  8. word = []
英文:
  1. 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" }
  2. word = []
  3. while True:
  4. converter = input("Convert decimal to binary: ")
  5. for i in converter:
  6. word.append(binary.get(i))
  7. print(word)
  8. 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

  1. 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:

确定