Unit 3.5, 3.6, and 3.7 Notes
- What is a Boolean?
- Using Logical Operators!
- Conditionals
- Nested Conditionals
- Analyzing Code Walkthrough
- Hacks Assignments:
grade1 = 90
grade2 = 65
grade3 = 60
grade4 = 75
grade5 = 95
print((grade1 + grade2 + grade3 + grade4 + grade5)/5)
print("1 > 2 or 5 < 12:", 1 > 2 or 5 < 12)
# Output TRUE using OR ^
# Output FALSE using NOT
print("7 > 8:", not 7 > 8)
# Output FALSE using AND
print("10 > 20:", 10 > 20 and False)
x = 20
y = 10
if x > y:
print("x is greater than y")
x = 0
y = 10
if x > y:
print("x is greater than y")
else:
print("x is not greater than y")
num1 = 100
num2 = 100
sum = num1 + num2
if sum == 200:
print("200")
else:
print(sum)
num1 = 99
num2 = 100
sum = num1 + num2
if sum == 200:
print("200")
else:
print(sum)
Nested Conditionals
- Consist of conditional statements within other conditional statements
- Uses if else inside if else
Analyzing Code Walkthrough
- #1: Outcome prediction: 4
- #2: Outcome prediction: Please come to retake up to a 90 next week at tutorial!
- #3: Outcome prediction: Looks great but lets see if we can cut down on sugar, we don't want diabetes!
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!")
}
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!")
}
else
{
console.log ("Amazing, you created a healthy lunch!!!")
}
}
}
hours = 11
if (hours < 8)
{
console.log ("This person is unexperienced, salary is 50k!")
}
else
{
if (hours < 10)
{
console.log ("This person's salary is 90k, and they are experienced")
}
else
{
if (hours >= 10)
{
console.log ("This person's salary is 150k, and they are experienced")
}
}
}
Same code, different output
hours = 6
if (hours < 8)
{
console.log ("This person is unexperienced, salary is 50k!")
}
else
{
if (hours < 10)
{
console.log ("This person's salary is 90k, and they are experienced")
}
else
{
if (hours >= 10)
{
console.log ("This person's salary is 150k, and they are experienced")
}
}
}
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"
cost = input("How much does this object cost?")
expired = input("Is this product expired or not?") # y or n
if (expired == "yes"):
print("This product is expired and is no good. :( ");
else:
if (cost > "50" and expired == "no"):
print("This product isn't expired but is too expensive!")
else:
if (cost < "25" and expired == "no"):
print("This product isn't expired and is a cheap product! :)")
else:
print("This product isn't expired and is a regular product.")
print("Cost: " + cost )
print("Expired: " + expired)
Same code, different output
cost = input("How much does this object cost?")
expired = input("Is this product expired or not?") # y or n
if (expired == "yes"):
print("This product is expired and is no good. :( ");
else:
if (cost > "50" and expired == "no"):
print("This product isn't expired but is too expensive!")
else:
if (cost < "25" and expired == "no"):
print("This product isn't expired and is a cheap product! :)")
else:
print("This product isn't expired and is a regular product.")
print("Cost: " + cost )
print("Expired: " + expired)
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
class Question:
def __init__(self, prompt, answer): # Creating own data type
self.prompt = prompt
self.answer = answer
quesList = [ # list with the question and MC answers
"Question 1.) Who is Roanldo playing for in the world cup?\n(a) Portugal\n(b) Argentina",
"Question 2.) What is 2+6\n(a) 8\n(b) 26",
"Question 3.) What is the color of the sky\n(a) green\n(b) blue",
"Question 4.) Who teaches this class?\n(a) Mr.Mortensen\n(b) Mr.Yeung",
"Question 5.) Who is the highest payed soccer player?\n(a) Messi\n(b) Mbappe\n(c) Ronaldo",
"Question 6.) What is the tallest building in the world?\n(a) Effiel Tower\n(b) CNN Tower\n(c) Burj Khalifa"
]
questions = [ # defining the correct answers and the question associated with that answer using the class from above
Question(quesList[0], "a"),
Question(quesList[1], "a"),
Question(quesList[2], "b"),
Question(quesList[3], "a"),
Question(quesList[4], "b"),
Question(quesList[5], "c")
]
def quiz(questions): # creating a function
score = 0 # setting base score
for question in questions:
print(question.prompt) # prints prompt with the mc
answer = input(question.prompt) # input for the answer
print("Your answer: " + answer)
if answer == question.answer:
score += 1 # adds one to the score if the answer was right, does nothing if not
print("you got", score, "out of", len(questions)) # prints the number of questions correct over the number of questions
if score > 3 and score == 6:
print("Wow good job, you got a perfect score! Genius?", 4 < 6)
else:
if score == 5:
print("Good job! Aim for a perfect score next! Genius?", 4 < 5)
else:
if score > 3 or score <= 4:
print("Not bad but not good either, maybe try again! Genius?", 4 < 4)
else:
if score < 3 or score == 3:
print("You failed! Better luck next time! Genius?", 3 < 0)
quiz(questions)
Same Code, Different Output
class Question:
def __init__(self, prompt, answer): # Creating own data type
self.prompt = prompt
self.answer = answer
quesList = [ # list with the question and MC answers
"Question 1.) Who is Roanldo playing for in the world cup?\n(a) Portugal\n(b) Argentina",
"Question 2.) What is 2+6\n(a) 8\n(b) 26",
"Question 3.) What is the color of the sky\n(a) green\n(b) blue",
"Question 4.) Who teaches this class?\n(a) Mr.Mortensen\n(b) Mr.Yeung",
"Question 5.) Who is the highest payed soccer player?\n(a) Messi\n(b) Mbappe\n(c) Ronaldo",
"Question 6.) What is the tallest building in the world?\n(a) Effiel Tower\n(b) CNN Tower\n(c) Burj Khalifa"
]
questions = [ # defining the correct answers and the question associated with that answer using the class from above
Question(quesList[0], "a"),
Question(quesList[1], "a"),
Question(quesList[2], "b"),
Question(quesList[3], "a"),
Question(quesList[4], "b"),
Question(quesList[5], "c")
]
def quiz(questions): # creating a function
score = 0 # setting base score
for question in questions:
print(question.prompt) # prints prompt with the mc
answer = input(question.prompt) # input for the answer
print("Your answer: " + answer)
if answer == question.answer:
score += 1 # adds one to the score if the answer was right, does nothing if not
print("you got", score, "out of", len(questions)) # prints the number of questions correct over the number of questions
if score > 3 and score == 6:
print("Wow good job, you got a perfect score! Genius?", 4 < 6)
else:
if score == 5:
print("Good job! Aim for a perfect score next! Genius?", 4 < 5)
else:
if score >= 3 and score <= 4:
print("Not bad but not good either, maybe try again! Genius?", 4 < 4)
else:
if score < 3 or score == 0:
print("You failed! Better luck next time! Genius?", 3 < 0)
quiz(questions)