codeHunter
codeHunter

Reputation: 47

Python: Why does while loop exclude the first value?

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 ))

Upvotes: 1

Views: 318

Answers (4)

John Matecsa
John Matecsa

Reputation: 94

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 ))

Upvotes: 1

Ken Schumack
Ken Schumack

Reputation: 719

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 ))

Upvotes: 1

PM 2Ring
PM 2Ring

Reputation: 55469

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. ;)

Upvotes: 0

sonu_chauhan
sonu_chauhan

Reputation: 335

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 :)

Upvotes: 1

Related Questions