3.5 to 3.7

Booleans

  • Data type with 2 possible values
    • True
    • False
  • Similar to binary

Conditionals

  • selection uses a condition that evaluates to true or false
  • Algorithm is a finite set of instructions that accomplish a specific task
  • Conditional Statements
    • Also known as "if" and/or "if else" statements

Nested Conditionals

  • Conditionals in the "else" portion of a conditional
    • Inception with conditionals

Participation (Nested Conditionals)

  • Calculate the total sum of two numbers, if it is equal to 200, print 200, if otherwise, print the sum.
num1 = 147
num2 = 153
sum = num1 + num2

if sum == 200:
    print(num1, " + ", num2, "= 200")
else:
    print(num1, " + ", num2, "=/= 200")
147  +  153 =/= 200

Analyzing Code Walkthrough

  • Without executing the code, find what the output should be, given the already assigned variable(s)
score = 82
if (score >= 90):
{
    console.log("You got an A, congrats!")
}
else
    {
        if (score >= 75)
        {
            console.log("Please come to retake up to a 90 next week at tutorial!")
            # This will be the output^^^^ (because the score is < 90 and > 75)
        }
        else
            {
                console.log("You have detention!")
            }
    }
protein = 25
carbs = 36
sugar = 11
if (carbs >= 55 || protein <= 20 || sugar >= 15)
{
    console.log("Your lunch is too unhealthy, please pick a new one")
}
else
{
    if (carbs < 35 || protein < 25)
    {
    console.log ("This lunch is alright but try to add some more carbs or protein")
    }
    else 
    {
    if (sugar >= 11)
    {
    console.log ("Looks great but lets see if we can cut down on sugar, we don't want diabetes!")
    # This will be the output^^^^
    }
    else
    {
        console.log ("Amazing, you created a healthy lunch!!!")
    }
    }
}

Writing Nested Code Activity

  1. Write a program that fits these conditions using nested conditionals:
    • If a person has at least 8 hours, they are experienced
    • If a person is experienced their salary is 90k, if they have ten hours or above their salary 150k
    • If a person is inexperienced their salary is always 50k
    • print the salary of the person at the end and whether they are experienced or not
hours = 10

if (hours < 8):
    print("You are inexperienced and your salary is 50k")
else:
    if (hours < 10):
        print("You are experienced and your salary is 90k")
    else:
        print("You are experienced and your salary is 150k")
You are experienced and your salary is 150k

Hacks Assignments:

Conditionals:

  • Write a program that fits these conditions using nested conditionals:
    • If the product is expired, print "this product is no good"
    • If the cost is above 50 dollars, and the product isn't expired, print "this product is too expensive"
    • If the cost is 25 dollars but under 50, and the product isn't expired, print "this is a regular product"
    • If the cost is under 25 dollars, print "this is a cheap product"

Boolean/Conditionals:

  • Create a multiple choice quiz that
    • uses Boolean expressions
    • uses Logical operators
    • uses Conditional statements
    • prompts quiz-taker with multiple options (only one can be right)
    • has at least 3 questions
  • Points will be awarded for creativity, intricacy, and how well Boolean/Binary concepts have been intertwined

Hacks Submissions

# Let the user type in a price

expired = input("Answer yes or no: Is the product expired?").lower()

if expired == "yes":
    expired = True
elif expired == "no":
    expired = False
else:
    print("Answer with yes or no")

cost = int(input("Type in the cost of your product in dollars (don't put units)"))

# Check if input is valid
if (cost < 0):
    print("Your price must be a positive integer")


if (expired == True): 
    print("This product is no good.")
else: 
    if (cost > 50) and (expired == False):
        print("This product is too expensive")
    else:
        if (cost > 25) and (cost <= 50) and (expired == False):
            print("This is a regular product")
        else:
            if (cost <= 25):
                print("This is a cheap product")
This product is too expensive
score = 0
num = 0
incorrect = []
q = ["Should I do my homework?", 
    "Should Piday be a National Holiday?", 
    "Is the Earth flat?", 
    "Is Azeem cool?"]
a = ["n", "y", "n", "y"]

for i in q:
    answer = input(i + " Y/N").lower()
    if answer.lower() == a[num]:
        score += 1
        num += 1
    else:
        incorrect.append(answer + i)

percent = score/len(q)*100
letters = ["A", "B", "C", "D", "F"]
percents = [100, 90, 80, 70, 60, 0]


for i in range(len(percents) - 1):
    if percents[i] >= percent > percents[i+1]:
        letter = letters[i]
        break


print("Congrats you got a: " + str(score) + "/" + str(len(q)) + " That's a " + str(percent) +"%, a " + letter)
for i in range(len(incorrect)):
    print("For question #" + str(q.index(incorrect[i][1:]) + 1) + ", \"" + incorrect[i][1:] + "\", you said \"" + incorrect[i][:1] + "\", but the correct answer was \"" + a[q.index(incorrect[i][1:])] + "\"")
Congrats you got a: 3/4 That's a 75.0%, a C
For question #4, "Is Azeem cool?", you said "n", but the correct answer was "y"