如何在二进制转换程序中 python 中的每 4 位后创建一个 space
How do I create a space after every 4 bits in python in a binary conversion program
import collections
from collections import Counter
d = int(input('Pick a number to convert to Binary: '))
def convert(n, Counter = 0):
Counter = 0
if n > 1:
convert(n//2)
print(n % 2, end = '')
print("Your number in Binary is")
convert(d)
我不知道如何在输出中每 4 位或每 4 个数字创建一个 space。我尝试使用计数器、for 循环和几乎所有我能想到的东西。我只想知道我将如何做到这一点。任何帮助将不胜感激,我只是迷路了。
尝试这样做:
def convert(n, counter = None):
if not counter:
counter = 0
counter += 1
if n > 1:
convert(n//2, counter)
if counter % 4 == 0:
print(" ", end="")
print(n % 2, end = '')
print("Your number in Binary is")
convert(d)
print("")
3456 的输出作为输入:
Your number in Binary is
1101 1000 0000
import collections
from collections import Counter
d = int(input('Pick a number to convert to Binary: '))
def convert(n, Counter = 0):
Counter = 0
if n > 1:
convert(n//2)
print(n % 2, end = '')
print("Your number in Binary is")
convert(d)
我不知道如何在输出中每 4 位或每 4 个数字创建一个 space。我尝试使用计数器、for 循环和几乎所有我能想到的东西。我只想知道我将如何做到这一点。任何帮助将不胜感激,我只是迷路了。
尝试这样做:
def convert(n, counter = None):
if not counter:
counter = 0
counter += 1
if n > 1:
convert(n//2, counter)
if counter % 4 == 0:
print(" ", end="")
print(n % 2, end = '')
print("Your number in Binary is")
convert(d)
print("")
3456 的输出作为输入:
Your number in Binary is
1101 1000 0000