0
Intermediate Python - 16.2 Practice help - âdecimal to binaryâ
Please help, thanks! 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 ââââââââââ Current code: def convert(num): return (num % 2 + 10 * convert(num // 2))
2 RĂ©ponses
+ 1
Try it:
def convert(num, s = ""):
if num >1:
s = str(num%2)+s
else:
s = "1"+s
return s
return convert(num//2, s)
print(convert(int(input())))
- 1
Brilliant, thanks Daniel !