+ 1
Recursion
Having trouble building this conversation code using recursion. What am I missing? def convert(num): if num == 0: return 0 return (num % 2 + 10 * convert(num // 2)) a=int(input()) print(convert (a))
2 Respuestas
+ 1
# Copy, past and run me
def convert(num):
if num == 0:
return 0
return (num % 2 + 10 * convert(num // 2))
a=int(input())
print(convert (a))
'''
https://www.sololearn.com/Discuss/3080341/?ref=app
'''
+ 1
Only the first 4 lines are the conversion function. Python uses indentation to signify which lines of code belong where. Since your last two lines are indented, python sees them as part of your function. But they shouldn't be. To fix you need to remove the indentation on those two lines.
In your function, the third line is because of its indentation part of the if branch. But it should be the else branch, i.e. the branch that is executed if num is not equal to 0. Remove one level of indentation on that third line (to align with the if statement) or add an 'else:' between the two return statements, aligned with the if.