68 245
Assignments Done
Successfully Done
In November 2018

Answer to Question #64544 in Python for Abdulganiyu Kamaldeen

Question #64544
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
Expert's answer
def binary_converter(n):
if not (isinstance(n, int)) or not (0 <= n and n < 256):
return "Invalid input"
ans = ""
while n > 0:
dig = '1' if n % 2 else '0'
ans = dig + ans
n = n // 2
if not ans:
ans = "0"
return ans

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

Privacy policy Terms and Conditions