Changing decimal to binary-Recursion Python Intermediate | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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)))

11th Mar 2022, 12:51 PM
Cathyboum
2 Answers
+ 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
11th Mar 2022, 1:01 PM
Raul Ramirez
Raul Ramirez - avatar
0
num=int(input( )) def convert(num,lst=[]): if num==0: return l else: lst.append(num%2) return convert(num//2,lst)
11th Mar 2022, 4:27 PM
Oma Falk
Oma Falk - avatar