1

I am trying to make a decimal to binary converter, however, I noticed that some values are being ignored and the last value is not being entered into the list I created.

#Here we create a new empty list.
binary = []
n = int(input("Enter number: "))

while n > 1:
    n = n//2
    m = n%2
    binary.append(m)

binary.reverse()
print( " ".join( repr(e) for e in binary ))
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
codeHunter
  • 47
  • 6
  • Your condition is `n > 1`. What's going to happen when n is 1? – user2357112 Oct 13 '17 at 02:58
  • If you're going to do both a division and a mod operation with the same divisor, you can probably improve performance slightly by using the `divmod` function to get both at once. – Blckknght Oct 13 '17 at 03:07
  • @Blckknght `divmod` isn't that fast, and it involves a function call, which is generally slower than using operators. But the exact speed difference is rather miniscule, and varies between versions. – PM 2Ring Oct 13 '17 at 03:09
  • 1
    @PM2Ring: Yeah, I probably shouldn't have mentioned performance in my comment. The real advantage of `divmod` is clarity. It says explicitly that you want both parts of a single division operation. It avoids issues like the question's code which has the separate operations in the wrong order, which causes it to lose the least significant bit of the input number. – Blckknght Oct 13 '17 at 18:13

4 Answers4

1

This is your code after correction :

binary = []
n = int(input("Enter number: "))
while n > 0:   
    m = n%2
    n = n//2
    binary.append(m)
if len(binary)==0:
    binary.append(0)
binary.reverse()
print( " ".join( repr(e) for e in binary ))

Your question is duplicate to this stackoverflow question check the link too.

good luck :)

sonu_chauhan
  • 315
  • 4
  • 11
1
n = int(input("Enter number: "))

print("{0:0b}".format(n))    # one-line alternate solution

if n == 0:                     # original code with bugs fixed
    binary = [0]
else:
    binary = []
    while n > 0:
        m = n%2
        n = n//2
        binary.append(m)
    binary.reverse()
print("".join( repr(e) for e in binary ))
1

As PM 2Ring suggested a tuple assignment may be the way to go. Makes your code shorter too :-) ... also changed n > 1 to n >= 1

binary = []
n = int(input("Enter number: "))
while n >= 1:
    n, m = n // 2, n % 2
    binary.append(m)
binary.reverse()
print( " ".join( repr(e) for e in binary ))
Ken Schumack
  • 719
  • 4
  • 11
0

Your algorithm is close, but you need to save the remainder before you perform the division. And you also need to change the while condition, and to do special handling if the input value of n is zero.

I've fixed your code & put it in a loop to make it easier to test.

for n in range(16):
    old_n = n
    #Here we create a new empty list.
    binary = []
    while n:
        m = n % 2
        n = n // 2
        binary.append(m)

    # If the binary list is empty, the original n must have been zero
    if not binary:
        binary.append(0)

    binary.reverse()
    print(old_n, " ".join(repr(e) for e in binary))

output

0 0
1 1
2 1 0
3 1 1
4 1 0 0
5 1 0 1
6 1 1 0
7 1 1 1
8 1 0 0 0
9 1 0 0 1
10 1 0 1 0
11 1 0 1 1
12 1 1 0 0
13 1 1 0 1
14 1 1 1 0
15 1 1 1 1

As Blckknght mentions in the comments, there's a standard function that can give you the quotient and remainder in one step

n, m = divmod(n, 2)

It can be convenient, but it doesn't really provide much benefit apart from making the code a little more readable. Another option is to use a tuple assignment to perform the operations in parallel:

n, m = n // 2, n % 2 

It's a good practice, especially when you're new to coding, to work through your algorithm on paper to get a feel for what it's doing. And if your code doesn't give the expected output it's a good idea to add a few print calls in strategic places to check that the values of your variables are what you expect them to be. Or learn to use a proper debugger. ;)

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182