Python Forum
An algorithm - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: An algorithm (/thread-876.html)



An algorithm - akulamartin - Nov-11-2016

Hey all once again how would you create a function called
binary_converter
. Inside the function, implement an algorithm to convert decimal numbers between
0 and 255
to their binary equivalents.
For any invalid input, return string Invalid input
Example: For number 5 return string
101

i may have found a possible solution
def convertToBinary(n):
  """Function to print binary number
  for the input decimal using recursion"""
  if n > 1:
      convertToBinary(n//2)
  print(n % 2,end = '')

# decimal number
dec = 34

convertToBinary(dec)



RE: AN ALGORITHM - Larz60+ - Nov-11-2016

Please use code tags


RE: An algorithm - ichabod801 - Nov-11-2016

And do you have a question? I note that you are not handling invalid input as per the requirements.


RE: An algorithm - sparkz_alot - Nov-11-2016

Any reason you can't use "bin(n)" in your def?


RE: An algorithm - nilamo - Nov-11-2016

If you're supposed to return something, then you shouldn't be printing anything within the function. Also, you didn't give it the name you should have. Your professor would be disappointed.