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))

8th Jul 2021, 5:13 PM
Zach Z
2 Respostas
+ 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())))
8th Jul 2021, 6:17 PM
Daniel
Daniel - avatar
- 1
Brilliant, thanks Daniel !
9th Jul 2021, 6:51 PM
Zach Z