Fall 2024 - P3
3.7 Homework Question
Test your understanding of nested conditionals
Testing Your Skills (HW Question #1)
- Create a simple system using
if
andelse
statements that determines whether or not a user (based on their age) is eligible to vote - If the person is less than 18 years old, they should not be able to vote
- If the person is 18 years old or older, they are able to vote
- You should get something like this:
Explanation
- The if statement checks if the user’s age is greater than or equal to 18. If they are 18, that means that the statement is true and will print “You are eligible to vote.” If the age requirement is not met, that means the statement will be false and the else statement will print “You are not eligible to vote.”
Quick Quiz
What would the function output if the user’s age was 16?
%%html
<div id="answer" style="display:none; border: 1px solid; padding: 10px; margin-top: 10px;">
<b>Answer:</b> It would say, "You are not eligible to vote."
</div>
<button onclick="document.getElementById('answer').style.display='block'; this.style.display='none';">
Reveal Answer
</button>
When the age of the user is greater than 18, what is the condition in the if statement?
%%html
<div id="answer1" style="display:none; border: 1px solid; padding: 10px; margin-top: 10px;">
<b>Answer:</b> It would say, "You are eligible to vote."
</div>
<button onclick="document.getElementById('answer1').style.display='block'; this.style.display='none';">
Reveal Answer
</button>
What is the purpose of the if statement in the function?
%%html
<div id="answer2" style="display:none; border: 1px solid; padding: 10px; margin-top: 10px;">
<b>Answer:</b> To determine if you are eligible to vote!
</div>
<button onclick="document.getElementById('answer2').style.display='block'; this.style.display='none';">
Reveal Answer
</button>
HW Question #2
- Create a simple system using
if
andelse
statements that determines what the user would eat - If the person is healthy, make them eat an apple
- If the person doesn’t care about what they eat, make them drink coffee
- If the person is unhealthy, make them eat chocolate
- You should get something like this:
# Get user input
health_status = input("Are you healthy, unhealthy, or indifferent about food? (healthy/unhealthy/indifferent): ").lower()
if health_status == "healthy":
print("Treat yourself to chocolate!")
else:
# Check for the other conditions within the else block
if health_status == "unhealthy":
print("You should eat an apple!")
else:
print("You don't care about what you eat. How about a coffee?")
You should eat an apple!
Explanation
- The if statement checks if the user’s health status is “healthy.” If this condition is true, the program will print “You should eat an apple!” indicating a healthy choice.
- If the health status is not “healthy,” the else statement is executed. Within this block, another if statement checks if the user’s health status is “unhealthy.” If this condition is true, the program will print “You should eat chocolate!”
- Lastly, if neither condition is met when the user doesn’t care about what they eat, the else statement will execute, printing “You don’t care about what you eat. How about a coffee?” This covers the case for users who are indifferent about their food choices. Keep in mind, you can also achieve this same result using
elif statements
instead of having to create chains ofif
andelse statements
.
# Get user input
health_status = input("Are you healthy, unhealthy, or indifferent about food? (healthy/unhealthy/indifferent): ").strip().lower()
if health_status == "healthy":
print("You should eat an apple!")
elif health_status == "unhealthy":
print("You should eat chocolate!")
elif health_status == "indifferent":
print("You don't care about what you eat. How about a coffee?")
else:
print("Invalid input! Please enter 'healthy', 'unhealthy', or 'indifferent'.")
- Notice how there’s no inner constructs in this code, so it does not contain nested conditionals!
Quick Quiz
What would the function output if the user’s age was not healthy?
%%html
<div id="answer3" style="display:none; border: 1px solid; padding: 10px; margin-top: 10px;">
<b>Answer:</b> It would say, "You should eat an apple!"
</div>
<button onclick="document.getElementById('answer3').style.display='block'; this.style.display='none';">
Reveal Answer
</button>
When the age of the user is unhealthy, what is the condition in the if statement?
%%html
<div id="answer4" style="display:none; border: 1px solid; padding: 10px; margin-top: 10px;">
<b>Answer:</b> It would say, "Treat yourself to chocolate!"
</div>
<button onclick="document.getElementById('answer4').style.display='block'; this.style.display='none';">
Reveal Answer
</button>