Unit 3.5 - 3.7 Hacks
Notes
Boolean
- This is a test
- Represents logical propositions
- Either true or false, such as testing for two numbers being equal
- Represented with variables
Logical Operators
- Not displays the opposite
- And is used for two conditions together
- Or is when using the function to see if one condition is met
Nested Conditional
- Output changes based on what is written
- Nested conditional statements have a coditional statement inside of another conditional statement.
- TCalled nested conditional because they are nested together,
- Can be used for varying "else if statement"
Hacks
3.5
- Explain in your own words what each logical operator does
- Not statement will display based on if the result is the opposite of the data.
- And Statement will display only if both of the conditions within the code are met.
- Or statements will display if at least one of the conditionals within the code are met.
- Code your own scenario that makes sense for each logical operator
isJuly = False
if not isJuly:
print("It is not july")
grade = 95
if grade < 100 and grade >= 90:
print("You have an A")
window = "open"
door = "closed"
if window == "open" or door == "open":
print("there is a way out")
else:
print("There is not a way out")
3.6
- Selection: Code that is only executed if true or if false.
- Algorithm: Code that has instructions in which its goal is to complete something.
- Conditional Statement: Code that runs depending on if the result is true or false.
- For the first hack, pretend you are a school's test grader. Create an array with integers, each integer representing one score from a student's taken tests. If the average of the student's test scores are at least 75 percent, then display that the student is elligible for credit, and if not, display that the student must retake the tests over break.
grades = [78, 85, 56, 99, 96]
average = sum(grades) / len(grades)
if (average > 75):
print("eligible for credit")
else:
print("not eligible for credit")
- The second hack is more number-oriented. Create an algorithm that calculates the sum of two numbers, then determines whether the sum is greater than or less than 100.
grades = [78, 33]
scoreSum = sum(grades)
if scoreSum > 100:
print("greater than 100")
text = "I am Nathan"
for lets in text:
if lets == "a" or lets == "e" or lets == "i" or lets == "o" or lets == "u":
print(lets)
carColor = "black"
bodyStyle = "sedan"
if (carColor == "black"):
if (bodyStyle == "sedan"):
print("It is a black sedan")
else:
print("It is a black car but not a sedan")
if (carColor != "black"):
if (bodyStyle == "coupe"):
print("It is a coupe but it is not black")
else:
print("it is neither a black car nor a coupe")
grade = 89
if (grade > 80):
if (grade > 90):
print("Keep it up!")
else:
print("Not too bad")
if (grade < 80):
if (grade < 60):
print("You really need to work on your grades")
else:
print("You need to step it up")
Location = "USA"
State = "Michigan"
Planet = "Earth"
if (Location == "USA"):
if (State == "California"):
print("You live in cali")
else:
print("You live in the USA but not the best state.")
if (Location != "USA"):
if (Planet == "Earth"):
print("Congrats, you live on Earth")
else:
print("Bro what")