-1

Here i declared value=value//2

def dec2bin(value,lis=[]):
    if value>1:
        value=value//2
        dec2bin(value)
    lis.append(value%2)
    return lis
    
print(dec2bin(5))

returns [1,1,0]

Here I just used value//2 within the function

def dec2bin(value,lis=[]):
    if value>1:
        dec2bin(value//2)
    lis.append(value%2)
    return lis
    
print(dec2bin(5))

returns [1,0,1]

1 Answers1

1

There are many things wrong here. The most surprising is probably "Least Astonishment" and the Mutable Default Argument which means that lis=[] probably doesn't do what you expect here.

Secondly, you are not recording the result from your recursive call.

Try this instead:

def dec2bin(value):
      lis = []
      if value > 1:
        lis = dec2bin(value//2)
      lis.append(value % 2)
      return lis
tripleee
  • 175,061
  • 34
  • 275
  • 318