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.


I added an input to take in a decimal number to convert to binary

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 = int(input("Enter num"))
print("Binary of num " + str(num) + " is:", end=" ")
reverse(DecimalToBinary(num))
Binary of num 32 is: 100000

Below are some other versions of a decimal to binary converter that I made.

Version #1

This version creates a list that stores the powers of 2 (from 2^0 to 2^7). Each number in the list is subtracted from the input decimal number if the decimal number is larger, and 1s and 0s are added accordingly to the binary string based on if the power of 2 is subtracted or not.

def convert(num):
    numOfBinary = 8
    binary = []; 
    output = ""
    
    for i in range(numOfBinary): 
        binary.append((2**(numOfBinary - 1 - i)))
    
    for i in range(len(binary)):
        if num - binary[i] >= 0:
            num -= binary[i]
            output += "1"
        else: 
            output += "0"
    return output;   
        
num = int(input("Enter a number"))

print(str(num) + " in binary is: " + convert(num))
32 in binary is: 00100000

Version #2 (Extra)

In this version, I created a different binary calculator that could repeatedly perform binary conversions. Once the user finishes entering all of the decimal numbers, the user can enter "y" to exit, and the calculator will display all of the conversions.

def binaryCalc(num): 
    binaryNum = 0; 
    count = 0;
    binaryString = ""; 
    while binaryNum < num:
        binaryNum = 2**count 
        count += 1
    #print(count) # count = 5

    count -= 1
    while (num > 0 and count >= 0): 
        if num >= 2**count: 
            num -= (2**count)
            binaryString += "1"
            count -= 1
        else: 
            binaryString += "0"
            count -= 1
    count += 1
    #print("Count" + str(count))
    
    if count > 0: 
        while count > 0:
            binaryString += "0"
            count -= 1
    #print("binary" + binaryString)
    return binaryString
    
binaryList = []
def saveBinary(binary):
    binaryList.append(binary)



end = False 
while (end == False):
    num = int(input("Enter a number: "))
    print("Enter a number")
    print(num)
    calculation = binaryCalc(num)
    saveBinary(calculation)
    response = input("Would you like to stop? y/n")
    print("Would you like to stop? y/n")
    print(response)
    if (response == "y"):
        end = True 

print("History: " + str(binaryList))
Enter a number
25
Would you like to stop? y/n
n
Enter a number
128
Would you like to stop? y/n
n
Enter a number
234
Would you like to stop? y/n
y
History: ['011001', '10000000', '011101010']


Lesson activities

print("True:",4 == 4)
print("True:",1 > 0)
print("False:",7 < 3)
print("True:",5 != 6)
print("False:",7 >= 8)
print("True:",3 <= 3)
print('')

# Same as above, but now for other values other than int
print('True:',"as" == "as")
print("False",True <= False)
print("False:",[2,3,1] != [2,3,1])
print("True:",'af' < 'bc')
print("False:",'ce' > 'cf')
print("True:",[1,'b'] >= [1,'a'])
print('')
True: True
True: True
False: False
True: True
False: False
True: True

True: True
False False
False: False
True: True
False: False
True: True

print("True:", True or False)
print("False:",  not True)
print("True:", True  and True)
print("False:",  not True)
print("False:", True and False)
print("True:",  not False)
True: True
False: False
True: True
False: False
False: False
True: True
print( 3 >= 3 and 4 < 6 or 5 !=  7 )
True
age = 20 ; 
if (age >= 70): 
    print("Old")
else: 
    print("Not that old")
if (age > 50): 
    print("You have lived for more than half a century")
elif (age == 50):
    print("You have lived for half a century")
else:
    print("You have lived for less than half a century")
if (age >= 21): 
    print("You are old enough to drink")
else:
    print("You are not old enough to drink") 
if (age >= 18): 
    print("You are old enough to vote")
else: 
    print("You are not old enough to vote")
Not that old
You have lived for less than half a century
You are not old enough to drink
You are old enough to vote
def function(x, y, z):
    if(x > y):
    #Code here
        if(x > z):
            print("x is greater than y and z")
        #Code here
        else:
            print("x is greater than y but not z")
        #Code here
    else:
        if(x > z):
            print("x is greater than z but not y")
        #Code here
        else:
            print("x is less than y and z")
        #Code here
        
function(1, 2, 3)
x is less than y and z