I'm trying to write a script which converts a given integer number into a binary number using recursion. Here's the code that I've written:
def dec2bin(n):
if n > 0:
if n%2 == 0:
li.append(0)
else:
li.append(1)
return dec2bin(n/2)
else:
aa = li[::-1]
for e in aa:
print e,
n = int(raw_input())
li = []
dec2bin(n)
However, this code keeps on running and never outputs the correct answer. What seems to be the problem with this code?
nhere isn't a decimal number, it's anint, which has no inherent base (or, if you want to stretch it and say it has one, the inherent base is binary).keeps on running and never outputs the correct answerI think you are forgetting to enter an input forraw_input()and it just waits for your input.