0
Changing decimal to binary-Recursion Python Intermediate
I wrote this code which doesn't work. My idea is to print all the remainders as a list then reverse the list..... The base case is when num==1, return 1.... Can someone please help me? num=int(input()) def convert(num): if num==1: return 1 else: while num % 2 >= 0: return (num % 2) print(list(convert.reverse(num)))
2 Respostas
+ 1
The wile loop wouldnt work
It would exit the function right away with the return statement
Lets pretend it wouldnt have
That wile loop would result in an infinite loop
0
num=int(input( ))
def convert(num,lst=[]):
if num==0:
return l
else:
lst.append(num%2)
return convert(num//2,lst)