Practice Problem

cookies = 5467; 
freeCookies = 5
if cookies >= 10: 
    print("Wow that's a lot of cookies."); 
else: 
    print(". i"); 
if freeCookies >= 24: 
    print("Surely you have to have extras. Can I have one?"); 
else: 
    print("froth froth froth froth froth froth froth"); 
Wow that's a lot of cookies.
froth froth froth froth froth froth froth

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):
    return(strs[::-1])
    
def ReverseNumber(num):
    return reverse(DecimalToBinary(num))
 
# Driver Code
s = ""
while (s != "q"):
    print("\nEnter a number or type 'q' to quit.")
    s = input()
    try:
        if s != "q":
            num = (int(s))
            result = ReverseNumber(num)
            print("Binary of", (num), "is:", (result))
            print ("The length of", (result) , "is:", (str(len(result))))
            if (len(result)) >= 10:
                print ("Stream Crazy:B「指先のアリアドネ」 あんさんぶるスターズ!! Music ゲームサイズMV")
            else:
                print ("Stream Crazy:B & UNDEAD「U.S.A.」あんさんぶるスターズ!! Music ゲームサイズMV")
                
    except ValueError:
        print ("Invalid input.")

# my additions to the code that i accidently deleted twice but it's ok i screenshotted
# ignore how i only have like 1 commit I did this all on a python emulator because I didn't want to get my laptop
Enter a number or type 'q' to quit.
Binary of 23125 is: 101101001010101
The length of 101101001010101 is: 15
Stream Crazy:B「指先のアリアドネ」 あんさんぶるスターズ!! Music ゲームサイズMV

Enter a number or type 'q' to quit.
Binary of 4 is: 100
The length of 100 is: 3
Stream Crazy:B & UNDEAD「U.S.A.」あんさんぶるスターズ!! Music ゲームサイズMV

Enter a number or type 'q' to quit.
Invalid input.

Enter a number or type 'q' to quit.

Notes

  • Boolean - 2 options (ex: true/false, yes/no, 0/1)
  • Relational Operator - Operators that can work between any two values of the same type
  • Logical Operator - Operators that work on operands to produce a single boolean result (such as and or not)
  • Relational operators go first, then logical

  • All programs have conditionals

  • Conditionals drive selection; they dictate what happens in a program based on whether something is true or false
  • Usually lots of if/else statements
  • 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
  • If/else statements change based on what has occured before
  • Nested Conditionals are like conditionals within conditionals
  • (Ex: If condtion 1 is met, condition 2 will occur, and if condition 2 occurs, condition 3 can occur)