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.

num = int(input("Enter a decimal number to be converted into binary."))
def convert(num):
	
	if num >= 1:
		convert(num // 2)  # double slash : divide the first number by the second, rounds to the nearest integer
	print(num % 2, end = '') # end = '' signifies what will come between each printed num
 
# Driver Code
print("Binary of num " + str(num) + " is:", end=" ")
convert(num)
Binary of num 45 is: 0101101
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 < False)
print("True:", True == True)
print("False:", False > True)
print("False:", True == False)
print("True:",  False != True)
True: True
False: False
True: True
False: False
False: False
True: True
Age = int(input("What is your age?"))  
print("You are " + str(Age) + " years old.")
if (Age < 10): 
    print("You are very young")
else: 
    print("You're in the double digits!") 
if (Age > 50): 
    print("You're pretty old")
else: 
    print("You're still in your young years")

# ignore this comment
You are 32 years old.
You're in the double digits!
You're still in your young years

5: Boolean Expressions

  • Booleans are like binary, 2 possible values: True, False
  • Regional Operator: Works between two values that are the same type. ==, !=, > , < ,>=, <=
  • Logical Operator: operators produce one boolean result (True/False)

6: Conditionals

  • Algorithms: set of instructions to accomplish a task
  • Selection: process that determines which parts of an algorithm should be run based on a condition
  • Conditional is a statement that determines the flow of a program
  • If statements for example

7: Nested Conditionals

  • conditional statements within conditional statements
  • If/else within if/else (see below for example)
IF (condition ):
    <execute A>
ELSE
    IF (condition ):
        <execute B>
    ELSE:
        <Execute C>
num = -89
if num >= 0:
    if num == 0:
        print("Zero")
    else:
        print("Positive ")
else:
    print("Negative")
Negative
temp = 95
if (temp >= 90):
    print("it is too hot outside")
if (temp >= 65):
    print("i will go outside")
if (temp < 65):
    print("it is too cold outside")