4

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)
2
  • This is a practice question for my midterm coming up for school and we have not learned bin Commented Feb 26, 2016 at 16:18
  • 1- the input value is not decimal, binary, hex, or anything else: it is a positive integer (you can pass it using a literal in any base you like e.g., 0xa, 0b1010, 0o12, 10, etc). The output is a string that contains binary ("01") representation of the input integer. 2- use return '0' instead of return 0 if the input is 0. 3- use raise ValueError('expected nonnegative integer') instead of return 'Not positive' then you could use to_bin = lambda n: to_bin(n//2) + '01'[n%2] if n else '' for positive n Commented Feb 26, 2016 at 16:46

7 Answers 7

5

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
Sign up to request clarification or add additional context in comments.

Comments

2

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

0

What about bin?

>>> bin(12)
'0b1100'
>>> bin(0)
'0b0'
>>> bin(-1)
'-0b1'

1 Comment

Note if you're using 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. ;)
0

You should use bin(), a built-in python function

https://docs.python.org/3.1/library/functions.html#bin

Comments

0

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

That makes perfect sense. Thanks a ton!
Pleasure =). Good luck on your exams.
0

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

Remove the str conversion from the second implementation.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.