Boolean Expressions
Sections 5-7
What is a boolean?
-A data type with two possible values: true or false Boolean and Binary So similar yet so different.
-Boolean math and binary notation both use the same two ciphers: 1 and 0.
However, please note that Boolean quantities are restricted to a singlular bit (can only be either 1, or 0)
On the otherhand, binary numbers may be composed of many bits adding up in place-weighted form to any finite value, or size
Relational Operators in action
How could you use operators to determine if the average of 5 grades is greater than 80?
With the grades below, use a boolean expression to determine if the average grade is above an 80 and print the result (True or False)
Try it in as few steps as possible! Be creative! There are obviously TONS of different practical solution
grade1 = 90
grade2 = 65
grade3 = 60
grade4 = 75
grade5 = 95
grades = grade1 + grade2 + grade3 + grade4 + grade5
average = grades % 5
average > 80
if average == 0:
print("false")
else:
print("true")
Conditionals
Focusing on Selection Selection: uses a condition that evaluates to true or false
Selection determines which part of an algorithm are executed based on a condition being true or false
Algorithm is a finite set of instructions that accomplish a specific task
Conditional Statements Also known as "if statements"
Can be seen as if statements or if blocks
Can also be seen as if else statements or if else-blocks
x = 20
y = 10
if x > y:
print("x is greater than y")
x = 20
y = 10
if x > y:
print("x is greater than y")
else:
print("x is not greater than y")
num1 = 200
num2 = 300
sum = num1 + num2
print(str(sum) + "You don't need a condtional for this" )
var hrs = 10
var salary = ""
var experienced = true
if (hrs >= 10) {
salary = "150k"
}
else if (hrs >= 8) {
salary = "90k"
}
else {
salary = "50k"
experienced = false
}
console.log("This person has...\n" + "Salary: " + salary + "\n" + "Experience: " + experienced)
product = {"expired":false, "cost":10}
if (product["expired"] == true) {
console.log("This product is no good")
}
else {
if (product["cost"] > 50) {
console.log("This product is too really expensive!")
}
else if (product["cost"] > 25) {
console.log("This is a regular product")
}
else {
console.log(" This product is cheap")
}
}
class question:
def __init__(self, base, answer):
self.base = base
self.answer = answer
Questions = [
"This is the first question on the\n (t) True\n (f) False\n",
"What animal does bacon come from?\n (a) Pigs\n (b) Cows\n (c) Chicken\n",
"Did America get eliminated by the Netherlands\n (t) True\n (f) False\n",
"Was this test hard?\n (a) Yes extremely\n (b) No it was the easiest test of my life\n (c) I don't want to get this question right\n",
]
Q_Bank = [
question(Questions[0], "t"),
question(Questions[1], "a"),
question(Questions[2], "t"),
question(Questions[3], "b"),
]
def quiz(Q_Bank):
score = 0
for problem in Q_Bank:
print(problem.base)
rsp = input(problem.base)
if rsp.lower().strip() == problem.answer:
print("\nYour answer " + rsp + " matches the correct answer which was " + str(problem.answer))
score += 1
else:
print("\nYour answer " + rsp + " doesn't match the correct answer which was " + str(problem.answer))
if score >= 1:
print("You got " + str(score)+" out of " + str(len(Q_Bank)) + " points so you have passed this test.")
else:
print("You got " + str(score)+" out of " + str(len(Q_Bank)) + " points so you have failed this test.")
quiz(Q_Bank)