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(num):
    strs = ""
    while num:
        # if (num & 1) = 1
        if (num & 1):
            strs += "1"
        # if (num & 1) = 0
        else:
            strs += "0"
        # right shift by 1
        num >>= 1
    return strs
 
# function to reverse the string
def reverse(strs):
    print(strs[::-1])
 
# Driver Code
num = 67
print("Binary of num 67 is:", end=" ")
reverse(DecimalToBinary(num))
binary1 = 0
binary2 = 0
binary3 = 0
binary4 = 0
binary5 = 0
binary6 = 0
binary7 = 0
binary8 = 0
i = 0
while i < int(67):
    if binary1 == 0: binary1 += 1
    else:
        binary1 = 0
        if binary2 == 0: binary2 += 1
        else:
            binary2 = 0
            if binary3 == 0: binary3 += 1
            else:
                binary3 = 0
                if binary4 == 0: binary4 += 1
                else: 
                    binary4 = 0
                    if binary5 == 0: binary5 += 1
                    else:
                        binary5 = 0
                        if binary6 == 0: binary6 += 1
                        else:
                            binary6 = 0
                            if binary7 == 0: binary7 += 1
                            else:
                                binary7 = 0
                                if binary8 == 0: binary8 += 1
    i += 1
print(str(binary8) + str(binary7) + str(binary6) + str(binary5) + str(binary4) + str(binary3) + str(binary2) + str(binary1))
Binary of num 67 is: 1000011
01000011