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.

Decimal to Binary Converter Using division by 2

num = int(input("Enter the number you would like to convert into binary"))

def DecimalToBinary(num):
    if num > 1:
        DecimalToBinary(num // 2) #divide input by 2
        print(num % 2, end = "") #so that numbers prints all together instead of new line for each number

print("Your number, " + str(num) + ", in binary is:")
DecimalToBinary(num)
Your number, 1231, in binary is:
0011001111

Decimal to Binary Converter Using a While loop (Extra)

decimal = int(input("Enter the number you would like to convert into binary")) #get input from user & set variables
binary = 0
i = 0   #set initial
num = decimal

while(num > 0):
    binary = ((num%2)*(10**i)) + binary # set binary equal to remainder of the input/2, multiplied by 10^i
    num = int(num/2) #now number equal num/2 rounded to an integer
    i += 1 #incriment by 1

#output result       
print("Your number " + str(decimal) + " in binary is " + str(binary))
Your number 131 in binary is 10000011
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 > False)
print("False:",  True > True)
print("True:", True == True)
print("False:",  False == True)
print("False:", True == False)
print("True:",  False == False)
True: True
False: False
True: True
False: False
False: False
True: True
print(3,3,4,6,5,7)
3 3 4 6 5 7
def function(x, y, z):
    if x > y:
        if z > x:
            print("z is greater than everything else")
        else:
            print("x is greater than y and z")
    else:
        if y > x:
            print("y is greater than x!!!")
        else:
            print("i have no idea :(")

function(7, 12, 3)
y is greater than x!!!

Notes

  • Boolean Operators

    • Relational Operators can work between any two values of the same type known as operands, provided that the type supports such types of operators
    • operators ==, !=, >, <, >=, <=, each one working the same way that they work as one would expect, with == being the replacement for equal to, since = is reserved for value assignments.
    • They also work on other types, such as string or list, and values at each index is compared in order to determine which one is greater
    • for example: "abg"<"acd" returns True, since the computer first goes to a and a, after they are the same, they go to b and c, and since c is "greater" than b, "acd" is greater than "abg"
  • Vocab

    • algorithm - A set of instructions that accomplish a task.
    • selection - The process that determines which parts of an algoritm is being executed based on a condition that is true or false.
  • Conditional

    • A conditional is a statement that affects the flow/outcome of a program by executing different statements based on the result of a true or false statement. That true or false statement is a boolean expression.
  • If/el

    • can be if else, can also be just if
  • Nested conditionals

    • Nested Conditional Statements consist of conditional statements within conditional statements.