Unit 3 Lesson 5-7 Hacks and Notes
Boolean Expressions, Conditionals, Nested Conditionals
Notes:
- boolean
- true or false
- relational operators
- mathematical relationship between two variables
- a = b
- a > b
- logical operators
- not (opposite of whatever the data is)
- and (evaluate two conditions together and determine if both conditions are met)
- or (function only looks to see if one of the conditions is met wthen will)
- conditionals
- selection: specific block of coce that will execute depending on the algorithm condition returning true or false
- algorithm: finite set of instructions that accomplish a specific task
- conditional statement/if-statement: statement that affects the sequence of control by executing certain statments depending on the value of a boolean
- nested conditionals
- consists of conditional statements within conditional statements
- one inside the other
- can have three different conditions
Hacks:
Lesson 3.5
- Explain in your own words what each logical operator does:
- not: output is the opposite of the input given (ex. given true then NOT would display false)
- and: putting two conditions together and seeing if the input meets both conditions (ex. the number 24 must be less than 70 but greater than 20, this would be true)
- or: only checks to see if one of the given conditions are met for the input (ex. if money is less than 0 or age is greater than 25 then -1 would end game)
- Code your own scenario that makes sense for each logical operator
print("NOT:")
isSunny = False
result = not(isSunny)
print(result) # will print true bc opposite of false with not in result
print("")
print("AND:")
age = 25
height = 5
if age <= 30 and height >= 4:
print("You may proceed.") # needs to meet both requirements of age and height
print("")
print("OR:")
age = 20
money = 100
if age >= 18 or money <= 500:
print("You are eligable.")
Lesson 3.6
- 1 point for defining all key terms in your own words
- selection: depending on the conditiion using true or false, select a block of code to execute
- algorithm: a block of code that is like instrucutions to complete a task
- conditional statment/if-statment: code that makes decisions of how code is executed based on the if the condition given is true or false
- 1 point for each challenge that your program or pair program
- challenge 1:
inputstring = input("Type any sentence.")
def vowel(inputstring): # define function
count = 0
for char in inputstring:
if char == "a" or char == "e" or char == "i" or char == "o" or char == "u":
count = count + 1
print(count)
vowel(inputstring) # call function
input "hey my name is claire", output showing the number of vowels in the sentence.
age1 = input("Enter an age.")
age2 = input("Enter an age.")
if age1 == age2:
print("Match!")
else:
if age1 < age2:
print(age1 + " is less than " + age2)
elif age1 > age2:
print(age1 + " is greater than " + age2)
print("Success")
houseLocation = "san diego"
housePrice = 1000000
location = input("Where is the house you are looking at located?")
price = input("How much is the house?")
if houseLocation == location:
if housePrice > int(price):
print("Purchase House")
else:
print("Not within budget")
else:
if housePrice > int(price)/3:
print("Purchase House")
else:
print("Not within budget or in right location")
print("Success")
testScore = input("What did you score on this test?")
if int(testScore) >= 70:
if int(testScore) >= 85:
print("Great Job! You passed with an amazing score.")
else:
print("Good Job! You passed.")
else:
print("You did not pass.")
print("Success")
- create a piece of code that displays four statements instead of three
sleep = input("How many hours of sleep did you get last night?")
if int(sleep) >= 7:
if int(sleep) >= 9:
print("Wow! That is an amazing amount of sleep.")
else:
print("Nice! That is a good amount of sleep.")
else:
if int(sleep) <= 4:
print("Oh no, that is not enough sleep.")
else:
print("Okay, that is an average amount of sleep.")
- make piece of code that gives three different recommandations for possible classes to take at a school based on two different condtions. these conditions could be if the student likes STEM or not.
stem = input("Do you enjoy taking STEM classes?")
if stem == "yes":
listYes = ["AP Calc AB", "APCSP", "AP Biology", "Engineering"]
print("Great! You should take:", listYes)
else:
listNo = ["APEL", "APUSH", "AP Lit", "AP Art"]
print("Okay! You should take:", listNo)