Reputation: 71
I'm trying to get the function to work, it is suppose to convert Decimal to Binary but all it gives me different numbers instead. Like if I enter 12, it would give me 2. I'm not sure where in the code my issue is located. Any help would be great, thank you!
def decimalToBinary(value):
if value < 0: #Base case if number is a negative
return 'Not positive'
elif value == 0: #Base case if number is zero
return 0
else:
return decimalToBinary(value//2) + (value%2)
Upvotes: 4
Views: 6354
Reputation: 442
This may also work
def DecimalToBinary(number):
#This function uses recursion to convert & print decimal to binary number
if number > 1:
convertToBinary(number//2)
print(number % 2,end = '')
# decimal number
decimal = 34
convertToBinary(decimal)
#..........................................
#it will show output as 110100
Upvotes: 5
Reputation: 2120
I would take another aproach:
def decimalToBinary(number):
if number<0:
return 'Not positive'
i = 0
result = ''
while number>>i:
result = ('1' if number>>i&1 else '0') + result
i += 1
return result
Upvotes: 0
Reputation: 22021
You faced error because you adding numbers and not iterables but want to get bits sequences...,so you have to convert values you are adding to tuples or strings (or lists), see code below:
def decimalToBinary(value):
if value < 0: #Base case if number is a negative
return 'Not positive'
elif value == 0: #Base case if number is zero
return (0,)
else:
return decimalToBinary(value//2) + (value%2,)
print decimalToBinary(12)
I've replaced (value%2)
to (value%2,)
to create tuple(,
matter, for python it's mean creating a tuple, braces aren't do it... ) and return 0
to return (0,)
. However you can convert it to string too. For that replace (value%2)
to str(value%2
and 0
to str(0)
.
Note that you can use built-int bin
function ti get binary decimal:
print bin(12) # 'ob1100'
Good luck in your practice !
Upvotes: 2
Reputation: 18628
As an exercice, two way :
with strings:
def decimalToBinary(value):
if value < 0: #Base case if number is a negative
return 'Not positive'
elif value == 0: #Base case if number is zero
return ''
else:
return decimalToBinary(value//2) + str(value%2)
with ints:
def decimalToBinary(value):
if value < 0: #Base case if number is a negative
return 'Not positive'
elif value == 0: #Base case if number is zero
return 0
else:
return 10*decimalToBinary(value//2) + value%2
In this case, you obtain a decimal whose digits are only 1 or 0.
Upvotes: 0
Reputation: 709
Your problem is that you're adding up the digits of your result in decimal. So if your result was going to be 01100
, your function is outputting 0+1+1+0+0
which is equal to 2
. If you want your digits to stack up, then convert your results to string
and concatenate:
def decimalToBinary(value):
if value < 0: #Base case if number is a negative
return 'Not positive'
elif value == 0: #Base case if number is zero
return 0
else:
return str(decimalToBinary(value//2)) + str((value%2))
print decimalToBinary(12) # prints '01100
I am assuming you're doing this for practice, and that you're aware of python's built in function bin()
that already does that for you.
Upvotes: 0
Reputation: 1520
You should use bin()
, a built-in python function
https://docs.python.org/3.1/library/functions.html#bin
Upvotes: 0
Reputation: 3689
What about bin
?
>>> bin(12)
'0b1100'
>>> bin(0)
'0b0'
>>> bin(-1)
'-0b1'
Upvotes: 0