Create a simple system using if and else 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<divid="answer"style="display:none; border: 1px solid; padding: 10px; margin-top: 10px;"><b>Answer:</b>Itwouldsay,"You are not eligible to vote."</div><buttononclick="document.getElementById('answer').style.display='block'; this.style.display='none';">RevealAnswer</button>
Answer: It would say, "You are not eligible to vote."
When the age of the user is greater than 18, what is the condition in the if statement?
%%html<divid="answer1"style="display:none; border: 1px solid; padding: 10px; margin-top: 10px;"><b>Answer:</b>Itwouldsay,"You are eligible to vote."</div><buttononclick="document.getElementById('answer1').style.display='block'; this.style.display='none';">RevealAnswer</button>
Answer: It would say, "You are eligible to vote."
What is the purpose of the if statement in the function?
Create a simple system using if and else 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()ifhealth_status=="healthy":print("Treat yourself to chocolate!")else:# Check for the other conditions within the else block
ifhealth_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 of if and else statements.
# Get user input
health_status=input("Are you healthy, unhealthy, or indifferent about food? (healthy/unhealthy/indifferent): ").strip().lower()ifhealth_status=="healthy":print("You should eat an apple!")elifhealth_status=="unhealthy":print("You should eat chocolate!")elifhealth_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<divid="answer3"style="display:none; border: 1px solid; padding: 10px; margin-top: 10px;"><b>Answer:</b>Itwouldsay,"You should eat an apple!"</div><buttononclick="document.getElementById('answer3').style.display='block'; this.style.display='none';">RevealAnswer</button>
Answer: It would say, "You should eat an apple!"
When the age of the user is unhealthy, what is the condition in the if statement?