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)
7 Answers 7
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
Comments
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 !
Comments
What about bin?
>>> bin(12)
'0b1100'
>>> bin(0)
'0b0'
>>> bin(-1)
'-0b1'
1 Comment
bin for exam practice you should probably ignore its answers on negative numbers, since your prof probably wants you to understand how the negative numbers are actually stored, not just put a minus sign in front of the positive representation. ;)You should use bin(), a built-in python function
Comments
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.
2 Comments
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.
1 Comment
str conversion from the second implementation.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
0xa,0b1010,0o12,10, etc). The output is a string that contains binary ("01") representation of the input integer. 2- usereturn '0'instead ofreturn 0if the input is0. 3- useraise ValueError('expected nonnegative integer')instead ofreturn 'Not positive'then you could useto_bin = lambda n: to_bin(n//2) + '01'[n%2] if n else ''for positiven