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
4 Réponses
+ 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))
+ 3
Where's your attempt?
+ 1
your code works fine...
what's the expected output difference with your output wich makes you saying that "the code has an error"?
0
So remind me why we need print?