Scratchers - Boolean Logic in Programming PY Hacks
Boolean Hacks For Students To Try In Python
🔑 Boolean Hacks in Python: A Few New Codes!
This notebook has short Boolean challenges. Edit the code where it says TODO to make it work.
Challenge 1: Positive Number
Fix the condition so it prints num is positive if the number is greater than 0.
num = 3 # Try changing this number!
# TODO: change the condition
if num < 0: # <-- wrong, fix this
print(num, "is positive")
else:
print(num, "is NOT positive")
3 is NOT positive
Challenge 2: Is Even?
Change the condition so it prints num is even when the number is divisible by 2.
num = 7
# TODO: check if num is even
if num % 2 == 0: # <-- not correct
print(num, "is even")
else:
print(num, "is odd")
7 is odd
Challenge 3: Teenager Check
Print Teenager if age is between 13 and 19 (inclusive). Otherwise print Not Teenager. Fix the condition.
age = 20
# TODO: fix the condition
if age > 12 and age < 20: # <-- wrong
print("Teenager")
else:
print("Not Teenager")
Not Teenager
Challenge 4: Lamp Logic
We have two switches: a and b. The lamp should turn ON if at least one is True.
Fix the condition.
a, b = True, False
# TODO: fix condition for at least one True
if a or b: # <-- wrong
print("Lamp is ON")
else:
print("Lamp is OFF")
Lamp is ON