-1

This python code's aim is to convert decimal number to binary number. I know I can use int() function in the code but I am not able to determine the process that is happening in the code.

def decimal_to_binary(n):
   if(n > 1):
       decimal_to_binary(n//2)

   print(n % 2 , end='')

print(decimal_to_binary(18))
# The output for it is : 10010None

It is said that when the program enters if condition , it will first the interprete the code inside the if statement so how can it perform the print(n % 2 , end='')simultaneously. According to me, I think this print(n % 2 , end='') will only print one value(0 or 1). To me, it seems recursion and the print statement after the if statement are being interpreted simultaneously. And secondly, why None is also printed with output?

Moon Cheesez
  • 2,489
  • 3
  • 24
  • 38
Rohan
  • 55
  • 6
  • 3
    You might want to decide if the function should print the value, or return the value. If you return the string, you can unittest. And it's trivially easy to print the string after it is returned (as you have done automatically, even though it was wrong for the way you wrote this function). – Kenny Ostrom May 10 '20 at 12:35
  • `None` is the return value of `decimal_to_binary(18)`, it is the default return value of functions that don't specify one with `return`. If you don't want to have it printed, just don't `print()` it, just call `decimal_to_binary(18)` at the end of your code, this function prints what it has to print itself. The rest of your question is really unclear to me. Anyway: there can't be anything interpreted simultaneously. If you wnat to understand what is going on, take a pencil and a piece of paper and follow the program. – Thierry Lathuille May 10 '20 at 12:36
  • Nitpick: `decimal_to_binary` doesn't convert decimal to binary: it converts a Python `int` to a binary string representation of that `int`. Python `int`s are stored in a form of binary, not in decimal. (When you do `decimal_to_binary(18)`, the conversion from the decimal numeric literal `18` to the internal binary format actually occurs at parsing time, long before `decimal_to_binary` is called.) – Mark Dickinson May 10 '20 at 12:40
  • 1
    it doesn't convert anything :) – Kenny Ostrom May 10 '20 at 12:43
  • @KennyOstrom: Good point. – Mark Dickinson May 10 '20 at 12:43

1 Answers1

0

The problem is "it did what you told it to do."
Your function prints, then returns a value of None.
You then printed the return value of the function (None).
It didn't help that your function never prints a newline.

You were expecting the function to finish printing the number, then execute a separate print for the newline. To do that, you could use a helper function to handle the recursion separately:

def print_decimal_to_binary(n):
    dec_to_bin_recursive(n)
    print()

def dec_to_bin_recursive(n):
    if(n > 1):
        dec_to_bin_recursive(n//2)
    print(n % 2 , end='')

print_decimal_to_binary(18)

Of course, you were also expecting the function to return a value, otherwise you would not have enclosed it in a print statement. You expected that for good reason, as that's how it should be done so that you can also do automated testing. If someone wants to print a string, that's a separate and trivial task.

def decimal_to_binary_string(n):
    if not n:
        return ''
    return decimal_to_binary_string(n//2) + str(n%2)

print (decimal_to_binary_string(18))

It's important to return a value so you can write unit tests. We don't want to run this and check the binary representation. We want the computer to do that for us. Look up unit testing in pythong. Here's a gross oversimplification:

assert('10010' == decimal_to_binary_string(18))

of course, this is all silly, as this task is already implemented in python, see Convert decimal to binary in python

print(bin(18)[2:])
print('{0:b}'.format(18))
Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
  • Normally I wouldn't post working code for what might be a class assignment, but this particular code is all over stackoverflow, and multiple other places. So that made it easier to point out some things in your code. – Kenny Ostrom May 10 '20 at 18:32