Homework

  • toc: true
  • badges: true
  • comments: true
  • categories: [Week 13]

Notes

  1. In the Boolean value, Ture is 1 and False is 0.
  2. "==" is an alternative to equal to, since "=" is reserved for value assignment
  3. The relational operators come first and the logical operators come second, in the order of not, and, or
  4. In conditional, The true or false statement is a Boolean expression
  5. Flow Chart of a Nested Conditional Statement: in conditonal 1, "no" goes to statement, "yes" goes to condition 2. Condition 2 is the same

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))
Binary of num 67 is: 1000011
def DecimalToBinary(num):
    if num > 0 :
        DecimalToBinary(int(num / 2))
        print(num % 2, end='')
        
num = int(input("Enter a decimal number \n"))
DecimalToBinary(num)
1100010