Recursion Python Challenge, please help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answers
+ 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