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 个数字创建一个空格。我尝试使用计数器、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
我也想解决这个问题。
在阅读了您的请求和其他答案后,我有动力在我的上下文中解决它。期望这是一个改进。
我得到了灵感,然后基于有问题的代码和之前的答案:
https://stackoverflow.com/a/69776706/26970467
这是从中得出的解决方案,也满足了我在这段代码下面概述的愿望:
# Created code is made for Python 3.12
# blank line to start the output lower in the console window
print("")
# Define variables
# get number, define and initialize d variable in the global scope
d = int(input('Pick a number to convert to Binary: '))
# Define function convert
def convert(n, counter=None, results=None, store_n=None):
if not store_n:
# initialize the store_n variable as the input n passed into the function.
store_n = n
if not results:
# initialize the results variable as a list.
# this allows the recursion to persist till function completes.
results = []
if not counter:
# initialize the counter to zero.
counter = 0
# check if it is time to add a space to the result list. If so, do it.
# if counter is not the initial value proceed.
if counter != 0:
# if counter is a multiple of 4, with remainder of zero, add space to results list.
if counter % 4 == 0:
results.append(" ")
# increment counter by one
counter += 1
# put result of this recursive step into results list.
# result is mod 2 of the current n in this recursive step.
# the remainder of what n divided by 2 is. Remainder is either a 1 or a 0.
results.append(str(n % 2))
# check if this is a large number that requires more than 1 recursive step. if so, proceed.
if n > 1:
# pass next recursive step of n // 2 to function.
# The floor division operator here returns n divided by 2 without any remainder.
# also, passing current counter value, results and store_n static values into the recursive step.
convert(n // 2, counter, results, store_n)
# now we have all the results in a list but in reverse due to the recursion stepping.
# we will reverse the sequence of collected results and make them a new string to
# prevent the recursion from breaking the format.
formated_results = str(''.join(list(reversed(results))))
# get the length of the formated results string to determine how many digits exist in the result.
# gather information to test and add zeros to the beginning of the formated_results string.
# frl is a synonym for formated results length.
frl = len(formated_results)
# calculate the number of preceding zeros that are missing from the sequence of characters.
missing_zeros = 5-((frl - 4) % 5)
# calculate the number of digits that exist to the left of the first space.
number_of_characters_beyond_the_seperator_space = ((frl - 4) % 5)-1
# test and add zeros to the beginning of the formated_results string if needed.
if number_of_characters_beyond_the_seperator_space > 0:
formated_results = ("0" * missing_zeros) + formated_results
return formated_results
# Main code:
# blank line
print("")
# give result
print("Your number", d, "in Binary is", convert(d))
对于我的用例情况,我喜欢在输出中调用更多约束:
此外,您还会看到我对代码进行了注释,以便更好地了解代码的工作原理。
希望这对某人有帮助。