Python: binary count without using inline functions

Recently, I had problems creating a program that is calculated in binary format from 1 to the selected number.

This is my code at the moment:

num6 = 1
binStr = ''
num5 = input('Please enter a number to be counted to:')
while num5 != num6:
    binStr = str(num6 % 2) + binStr
    num6 //= 2

    num6 = num6 + 1

print(binStr)

For example, if I enter 5, he needs to go 1, 10, 11, 100, 101. It seems that I just do not understand. Any help would be appreciated, thanks.

+4
source share
1 answer

, num6, . , , num5 . binary_to_string :

num5 = int(input('Please enter a number to be counted to:'))
for i in range(num5 + 1):
    binStr = ""
    decimal_number = i
    while decimal_number > 0:
        binStr = str(decimal_number % 2) + binStr
        decimal_number //= 2
    print(binStr)
+1

All Articles