0

Recursion (Python Intermediate)

Can someone help here please, have no clue what to do here? The given code defines a recursive function convert(), which needs to convert its argument from decimal to binary. However, the code has an error. Fix the code by adding the base case for the recursion, then take a number from user input and call the convert() function, to output the result. Sample Input: 8 Sample Output: 1000

24th Jun 2021, 8:32 PM
Swetha
4 odpowiedzi
+ 2
Nevermind, this is it! def convert(num): if num==0: return 0 else: return (num % 2 + 10 * convert(num // 2)) inp = int(input()) print(convert(inp))
24th Jun 2021, 8:44 PM
Swetha
+ 3
Where's your attempt?
24th Jun 2021, 8:33 PM
Dino Wun (Use the search bar plz!)
Dino Wun (Use the search bar plz!) - avatar
+ 1
your code works fine... what's the expected output difference with your output wich makes you saying that "the code has an error"?
24th Jun 2021, 10:36 PM
visph
visph - avatar
0
So remind me why we need print?
27th Aug 2021, 7:37 PM
Anthony Amabile
Anthony Amabile - avatar