Question: Python 3.6- Please do the following to the original source code: Binary numbers will be respresented as text strings of only 1s or 0s, and
Python 3.6- Please do the following to the original source code: Binary numbers will be respresented as text strings of only 1s or 0s, and should always appear in strings that are multiples of eight characters long (length of 8, or 16, or 24, ....) In case the number to be represented does not require the full amount of the length, the leading positions are filled with 0s. For example 1101 is 11010000. And also provide detailed block comments explaining the following code. Do the leading zeroes and block comments to receive full credit. Thanks.
def decToBin(num): binary = [] while (num > 0): a = int(float(num % 2)) binary.append(a) num = (num - a) / 2 binary.append(0) string = "" for j in binary[::-1]: string = string + str(j) return (string) def binToDec(binstr): n = len(binstr) pad = 0 if n % 8 != 0: pad = n - (n // 8) * 8 for i in range(pad): binstr += "0" num = int(binstr, 2) return num def binToDec(binary): decimal = 0 for digit in binary: decimal = decimal * 2 + int(digit) return (decimal) def binAdd(): a = input("What is your first binary string? ") b = input("What is your second binary string? ") c = (binToDec(a)) + (binToDec(b)) return (decToBin(c)) def binSub(): a = input("What is your first binary string? ") b = input("What is your second binary string? ") c = (binToDec(a)) - (binToDec(b)) return (decToBin(c)) print("Option 1: Convert a decimal number to binary ") print("Option 2: Add two binary strings ") print("Option 3: Subtract two binary strings ") print("Option 4: Exit ") print("Option 5: Convert a binary number to decimal ") k = int(input("What would you like to do ? ")) if k == 1: print(decToBin(int(input("What is your number? ")))) elif k == 2: print(binAdd()) elif k == 3: print(binSub()) elif k == 4: quit() elif k == 5: print(binToDec(input("What is your binary number? "))) Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
