Recursion Python Challenge, please help | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 3

Recursion Python Challenge, please help

Hello guys, following: 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 This is my code so far: def convert(num): if num<=1: return 0 else: return (num % 2 + 10 * convert(num // 2)) bin=int(input()) convert(bin) iĀ“ve got a few questions, maybe you can help me solve them: -Is my Base case correct? I thought of choosing it <1 because otherwise it would keep up the division nearly infinite. But i didnt know what to return after it reaches <1 so i thought about zero what obviously isnt correct. -Do i have an input problem? i neither get any output. -Or did i do something wrong with the function. Would be cool if someone could help me out, really struggling with this one and couldnt find any help in the web. Thanks and have a good start in the week!

29th Mar 2021, 8:35 AM
Lucas Kutman
Lucas Kutman - avatar
9 Respostas
+ 4
Currently you don't have any output and don't save the conversion result. Replace last lines with dec = int(input()) bin = convert(dec) print(bin) Input is decimal, so I took the freedom to change the variable name
29th Mar 2021, 10:06 AM
Benjamin JĆ¼rgens
Benjamin JĆ¼rgens - avatar
+ 4
def convert(num): if num == 0 or num < 2: return 0 else: return (num % 2 + 10 * convert(num // 2)) print(convert(int(input()))
8th Sep 2021, 8:59 AM
Khomi TAKAYANAGI
Khomi TAKAYANAGI - avatar
+ 2
Why do you think returning 0 when num<1 isn't correct? Also it doesn't run nearly infinite, because recursion is done with num // 2, which is integer division: 7//2 = 6//2 = 3 5//2 = 4//2 = 2 3//2 = 2//2 = 1 1//2 = 0 So base case can either be if num==0: return 0 Or if num<2: return num
29th Mar 2021, 9:10 AM
Benjamin JĆ¼rgens
Benjamin JĆ¼rgens - avatar
+ 1
Benjamin JĆ¼rgens , right, but anyways do i have an output problem, do you have an idea whats wrong?
29th Mar 2021, 9:31 AM
Lucas Kutman
Lucas Kutman - avatar
+ 1
def convert(num): if num==0: return 0 elif num<2: return num else: return (num % 2 + 10 * convert(num // 2)) print(convert(int(input())))
9th Nov 2022, 7:20 PM
Hadi Mkammal
Hadi Mkammal - avatar
0
Benjamin JĆ¼rgens ahh thank you a lot. that was the problem. case closed, einen schƶnen tag dir und danke fĆ¼r die hilfe! :)
29th Mar 2021, 10:19 AM
Lucas Kutman
Lucas Kutman - avatar
0
Lucas Kutman šŸ‘ Dir auch einen schƶnen Tag!
29th Mar 2021, 10:35 AM
Benjamin JĆ¼rgens
Benjamin JĆ¼rgens - avatar
0
def convert(num): if num == 1: return 1 return (num % 2 + 10 * convert(num // 2)) num = int(input()) print(convert(num))
4th Feb 2023, 4:35 PM
LordRagnar
LordRagnar - avatar
0
def convert(num): if num<=1: return 1 else: return (num % 2 + 10 * convert(num // 2)) bin=int(input()) print(convert(bin))
11th Dec 2023, 7:10 PM
Junxia Wei