Boolean Project Solutions
Level 1: Turn the Lights Off
Level 2: What's the Weather
Level 3: Quiz Game
Level 1: Turn the Lights Off
Goal: Ask the user "Is it dark outside?" and depending on their answer, tell them to turn the lights on/off.
Step 1: Ask the user a question using the input() function, and save their answer as a variable
answer = input("Is it dark outside? (yes/no): ")
We created the variable answer to store the user's answer to the question, either "yes" or "no".
Step 2: Create a Boolean variable to convert the user's answer into a Boolean value
isDark = (answer == "yes")
The variable isDark stores a Boolean value (either True or False), using answer == "yes", which checks if the user's input (answer) is "yes", making isDark store True. If answer is "no" or anything other than "yes", isDark will be False.
Step 3: Decide with if/else what to print
if isDark == True:
print("Turn the lights on!")
else:
print("Turn the lights off!")
This if/else statement will check whether isDark is True or not, and prints accordingly. If isDark is true, that means the user answered "yes" to the question "Is it dark outside?", so we should print "Turn the lights on!". Otherwise, we should print "Turn the lights off!".
Level 2: What's the Weather?
Goal: Ask the user about the weather, and tell them how to dress appropriately.
Step 1: Ask the user questions using the input() function and save their answers as variables
raining = input("Is it raining? (yes/no): ")
cold = input("Is it cold outside? (yes/no): ")
windy = input("Is it windy? (yes/no): ")
We created 2 variables, raining and cold, to store the user's answers to each question.
Step 2: Create Boolean variables
isRaining = (raining == "yes")
isCold = (cold == "yes")
isWindy = (windy == "yes")
The variables isRaining, isCold, and isWindy now store Boolean values (True or False), and checks whether the user's output was "yes".
Step 3: Use if/elif statements as well as and, or, not to decide what message to print
if isRaining and isCold:
print("Wear a coat and bring an umbrella.")
elif isRaining and isWindy:
print("It's raining and windy, so wear a raincoat.")
elif isCold or isWindy:
print("Wear a jacket, it will feel chilly outside.")
elif not isRaining and not isCold and not isWindy:
print("Dress comfortably, it's not raining, cold, or windy!")
We use and, or, and not to combine conditions.
The first if statement uses and, which returns True if and only if both conditions are True. If it is raining and cold, bring a coat and an umbrella.
The following elif statement also uses and, to tell the user to wear a raincoat if it is raining and windy.
The next elif statement uses or, which returns True if at least one condition is True. If it is either cold or windy, wear a jacket.
The final elif statement uses and as well as not, which reverses each condition, meaning that if isRaining is False, not isRaining would be True. If it is not raining, cold, or windy, then dress comfortably.
Level 3: Quiz Game
Goal: Ask the user some multiple-choice questions, check if their answers are correct using Booleans, and keep a score.
Step 1: Create a list of questions as well as a list of answers. (add your own!)
questions = ["What color is the sky?", "How many letters are in the alphabet?", "How many sides does a pentagon have?"]
answers = ["blue", 26, 5]
We created 2 lists, questions and answers, that each have the same length. Each element in answers is the answer to the element it corresponds to in questions.
Step 2: Create a score variable
score = 0
Score will count how many questions the user gets right.
Step 3: Ask each question and save the user's answer
for i in range(len(questions)):
ans = input(questions[i]))
We use a for loop, range(), and len() to loop through indexes 0, 1, 2, ... so we can use i to access questions[i] and ask each question. The answer is then saved into ans using the input() function.
Step 4: Use a Boolean to check if the answer is correct, and add to the score
isCorrect = (ans == answers[i])
if isCorrect:
score += 1
The variable isCorrect will be True if the user's answer equals the answer to the question, otherwise it will be False. The if statement adds 1 point to the score if isCorrect is True.
Step 5: Print the final score
print("Your final score is " + score + " out of " + len(questions))
This prints how many answers were correct out of the total number of questions.