Homework/Hacks

our homework we have decided for a decimal number to binary converter. You must use conditional statements within your code and have a input box for where the decimal number will go. This will give you a 2.7 out of 3 and you may add anything else to the code to get above a 2.7.

Below is an example of decimal number to binary converter which you can use as a starting template.

def DecimalToBinary(input):
    if 0 <= input <= 255:
        pass
    else:
        print(int(input) + "Intentional Error to Restart Loop")
    i = 7
    binary = ""
    while i >= 0:
        if input % (2**i) == input:
            binary += "0"
            i -= 1
        else:
            binary += "1"
            input -= 2**i
            i -= 1
    return binary
 
# function to reverse the string
def reverse(strs):
    print(strs[::-1])
 
# Driver Code
num = 67
print("The reverse of 67 in binary is:", end=" ")
reverse(DecimalToBinary(num))

# input box
def getUserInput():
    try:
        userinp = int(input("Input an integer between 0 and 255."))
        userbin = DecimalToBinary(userinp)
        print('"' + str(userinp) + '" in binary is "' + userbin + '".')
    except:
        print("Invalid input.")
        getUserInput()

getUserInput()
The reverse of 67 in binary is: 11000010
"13" in binary is "00001101".