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):
    try: num=int(num)
    except: return """

    +=================================+
    | [ERROR] input must be a number! |
    +=================================+
    """
    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[::-1]

def operands(operation, num1, num2):
    bin1=DecimalToBinary(num1)
    bin2=DecimalToBinary(num2)
    if operation == "A":
        result = bin1|bin2
    elif operation == "B":
        result_or = bin1|bin2
        if result_or == 1:
            result = 0
        elif result_or == 0:
            result = 1
    elif operation == "C":
        result = bin1&bin2
    elif operation == "D":
        result_and = bin1&bin2
        if result_and == 1:
            result = 0
        elif result_and == 0:
            result = 1
    elif operation == "E":
        result = bin1^bin2
    elif operation == "F":
        result_xor = bin1^bin2
        if result_xor == 0:
            result = 1
        elif result_xor == 1:
            result = 0
    elif operation == "G":
        result = not bin1
    return result


# Driver Code
do = input("""
+=================================+
| [INFO] Select one:              |
| [A] Convert binary to decimal   |
| [B] Binary operations           |
+=================================+
    """)
if do.upper() == "A":
    num = input("What number do you want to convert to binary?")
    binary = DecimalToBinary(num)
    print(f"{num} converted to binary is: {binary}")
    exit()
elif do.upper() == "B":
    operation = input("""
    +=================================+
    | [INFO] Select one:              |
    | [A] OR                          |
    | [B] NOR                         |
    | [C] AND                         |
    | [D] NAND                        |
    | [E] XOR                         |
    | [F] XNOR                        |
    | [G] NOT                         |
    +=================================+
    """)
    while operation.upper() != "A" or operation.upper() != "B" or operation.upper != "C:" or operation.upper != "D:" or operation.upper != "E:" or operation.upper != "F:" or operation.upper != "G:":
        operation = input("""
    +=================================+
    | [INFO] Select one:              |
    | [A] OR                          |
    | [B] NOR                         |
    | [C] AND                         |
    | [D] NAND                        |
    | [E] XOR                         |
    | [F] XNOR                        |
    | [G] NOT                         |
    +=================================+
        """)
    if operation == "A":
        num1 = input("Input number 1:")
        num2 = input("Input number 2:")
        result = operands(operation, num1, num2)
    elif operation == "B":
        num1 = input("Input number 1:")
        num2 = input("Input number 2:")
        result = operands(operation, num1, num2)
    elif operation == "C":
        num1 = input("Input number 1:")
        num2 = input("Input number 2:")
        result = operands(operation, num1, num2)
    elif operation == "D":
        num1 = input("Input number 1:")
        num2 = input("Input number 2:")
        result = operands(operation, num1, num2)
    elif operation == "E":
        num1 = input("Input number 1:")
        num2 = input("Input number 2:")
        result = operands(operation, num1, num2)
    elif operation == "F":
        num1 = input("Input number 1:")
        num2 = input("Input number 2:")
        result = operands(operation, num1, num2)
    elif operation == "G":
        num1 = input("Input number 1:")
        result = operands(operation, num1, "")
    exit()
96 converted to binary is: 1100000
The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.