Unit 3.5/3.7 Hacks
activities = {
"tennis": True,
"football": False
}
# NOT
def is_not_playing(sport):
return not activities[sport]
# OR
def is_playing_sport():
return activities["tennis"] or activities["football"]
# AND
def has_no_time_for_homework():
return activities["tennis"] and activities["football"]
# MAIN LOGIC
if is_not_playing("football"):
print("you should play football!")
if is_playing_sport():
print("great job, sports are good!")
if has_no_time_for_homework():
print("wow, you're busy!")
3.6
Selection: only run some lines of code if a certain condition is true. One example of this is an if-then
statement.
Algorithm: a procedure of code that does something or a task. One example is a Python function that adds a certain constant to the integer provided as input and returns the new number.
Conditional statement: statement that affects whether a chunk of code will be executed or not. One example is an if-then
statement.
global lights
lights = 0b00000000
# light: light number from the left
def light_place(light):
return pow(2, 8-light)
def print_lights():
print('{:08b}'.format(lights))
def light_toggle(light):
global lights
# xor flips
lights ^= light_place(light)
def light_on(light):
global lights
# or always turns on
lights |= light_place(light)
def light_off(light):
global lights
# anding with mask (uses not) turns off
lights &= ~light_place(light)
## conditionals ##
def is_light_on(light):
global lights
return lights & light_place(light) == lights
def is_light_off(light):
global lights
return not is_light_on(light)
print_lights()
light_toggle(2)
print_lights()
light_on(7)
print_lights()
light_off(2)
print_lights()
print(is_light_on(7))
print(is_light_on(2))
forehand = 90
backhand = 50
serve = 75
name = input("what's ur name? ")
makes_team = True
if forehand < 70:
if backhand > 80:
if serve < 85:
makes_team = False
else:
makes_team = False
else:
if backhand < 85:
makes_team = False
elif serve < 70:
makes_team = False
if makes_team:
print("congrats", name, "you made it!")
else:
print("that's tough", name, "you didn't make it!")
motivation = 10
teacher = "Mortensen"
code_quality = 90
give_up = False
if teacher == "Mortensen":
give_up = True
else:
if motivation > 60:
give_up = False
else:
if code_quality < 80:
give_up = True
if give_up:
print("give up it's not worth it")
number = 2
guess = int(input("guess my number! "))
if guess < number:
print("too low")
else:
if guess == number:
print("just right")
else:
print("wayy too big")
phone = "iphone"
laptop = "macbook"
headphones = "airpods"
if phone == "iphone":
print("nice iphone")
if laptop == "macbook":
print("nice macbook")
if headphones == "airpods":
print("nice airpods")
print("you have 3 apple devices")
else:
print("you only have 2 apple devices")
else:
if headphones == "airpods":
print("nice airpods")
print("you have 2 apple devices")
else:
print("you only have 1 apple device")
else:
print("you should get an iphone")
print("answer: y/n")
calculus = input("Do you like calculus?")
writing = input("Do you like writing?")
physics = input("Do you like physics?")
CS = input("Do you like CS?")
print("calculus?:", calculus)
print("writing?:", writing)
print("physics?:", physics)
print("CS?:", CS)
if calculus == "y":
print("Take calculus 101")
if writing == "y":
print("Take writing 101")
if physics == "y":
print("Take physics 101")
if CS == "y":
print("Take CS 101")
elif writing == "y":
print("Take writing 101")
if physics == "y":
print("Take physics 101")
if CS == "y":
print("Take CS 101")
elif physics == "y":
print("Take physics 101")
if CS == "y":
print("Take CS 101")
else:
print("Take CS 101")