0

I have written code for converting decimal to binary using recursive method and not any built-in function. After executing the code I am getting the output along with 'None'. Here is the code for it.

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

I am getting the output as 111None.

Why am I getting None in the output ?

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
Chinmay
  • 1
  • 2
  • binrary returns nothing (i.e. `None`). you should add a `return` statement to the function or maybe better: just call `binary(7)` without the `print` statement. – hiro protagonist Oct 03 '20 at 10:00
  • Does this answer your question? [Python Function Returning None](https://stackoverflow.com/questions/21471876/python-function-returning-none) – Tomerikoo Oct 03 '20 at 11:46

1 Answers1

0

Every function returns something. If you do not write a return statement in the function, it returns "None".

Because your function already has print statements in it, just call it without print.
Aka:- binary(7).
Because print(binary (7)) prints the value returned by binary function after it has been executed, which in this case in "None".