Unit 3.5 - 3.7 Hacks
Lesson 3.5 Hacks
- Explain in your own words what each logical operator does
- NOT: returns the opposite of the data given, ex: if given true --> NOT would make it false
- AND: has two conditions and it sees if they are both met, ex: if a number is 5 --> must be greater than 0 AND less than 4, therefore false
- OR: determines of one of the two conditions set are met, ex: if a number is 5 --> must be greater than 0 AND less than 4, therefore true
- Code your own scenario that makes sense for each logical operator
print("NOT")
isHot = False
if not isHot:
print("It is cold")
print("")
print("AND")
age = 20
wallet = 50
ticket = 25
if age >= 18 and wallet >= ticket:
print("Is old enough and can afford a ticket")
print("")
print("OR")
member = True
escorted = False
if member or escorted:
print("Can enter the building")
Lesson 3.6 Hacks
- 1 point for defining all the key terms in your own words. 0.5 points if you use examples that show you truly understand it.
- Selection: if a condition is true, then a piece of code would execute
- Algorithm: a procedure of code that does something or a task
- Conditional statement: statement that dictates and affects the order code is executed if a condition is true
- 1 point for writing a program that uses binary conditional logic. 0.5 points if it is original and shows complexity
number1 = int(input("What is number 1?"))
number2 = int(input("What is number 2?"))
cond = input("AND, OR, XOR?").lower()
if cond == "and":
print("AND:", number1, "&", number2, "-->", number1 & number2)
elif cond =="or":
print("OR:", number1, "|", number2, "-->", number1 | number2)
elif cond =="xor":
print("XOR:", number1, "^", number2, "-->", number1 ^ number2)
else:
print("Error")
carColor = "blue"
carPrice = 20000
color = input("What car color?")
budget = input("What budget?")
if carColor == color:
if carPrice <= int(budget):
print("I will buy")
else:
print("Too expensive")
else:
if carPrice <= int(budget)/2:
print("I will buy because cheap")
else:
print("Wrong color and too expensive")
print("Thanks")
w = int(input("What is w?"))
z = int(input("What is z?"))
if w > z:
if w%2 == 0:
print(w, "is greater than", z, "and is even")
else:
print(w, "is greater than", z, "and is odd")
else:
if w%2 == 0:
print(w, "is less than", z, "and is even")
else:
print(w, "is less than", z, "and is odd")
print("Comparison")
t = int(input("What is t?"))
m = int(input("What is m?"))
if t == m:
print("Very Good", t, "=", m)
else:
if t < m:
print("Too Small", t, "<", m)
else:
print("Too Large", t, ">", m)
print("Done")
- Create a piece of code that displays four statements instead of three. Try to do more if you can.
age = int(input("What is your age?"))
grade = int(input("What is your grade?"))
print("age:", age)
print("grade:", grade)
if grade >= 1 and age >= 7:
if (grade + 6) == age:
print("You are normal")
elif (grade + 6) > age:
print("You are advanced")
else:
print("You are dumb")
else:
print("You are very young")
- Make piece of code that gives three different recommendations for possible classes to take at a school based on two different conditions. These conditions could be if the student likes STEM or not.
print("answer: y/n")
math = input("Do you like math?")
english = input("Do you like english?")
science = input("Do you like science?")
history = input("Do you like history?")
print("Math?:", math)
print("English?:", english)
print("Science?:", science)
print("History?:", history)
if math == "y":
print("Take Math 101")
if english == "y":
print("Take English 101")
if science == "y":
print("Take Science 101")
if history == "y":
print("Take History 101")
elif english == "y":
print("Take English 101")
if science == "y":
print("Take Science 101")
if history == "y":
print("Take History 101")
elif science == "y":
print("Take Science 101")
if history == "y":
print("Take History 101")
else:
print("Take History 101")