π Python Crash Course – Chapter 5: if Statements – Exercise 5-6 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 5-6
Task:
Write an if-elif-else chain that determines a person’s
stage of life . Set a value for the variable age, and then:
• If the person is less than 2 years old, print a message that the person is
a baby .
• If the person is at least 2 years old but less than 4, print a message that
the person is a toddler .
• If the person is at least 4 years old but less than 13, print a message that
the person is a kid .
• If the person is at least 13 years old but less than 20, print a message that
the person is a teenager .
• If the person is at least 20 years old but less than 65, print a message that
the person is an adult .
• If the person is age 65 or older, print a message that the person is an
elder
✅ Solution Code:
age = 50
if (age < 2 ):
print("baby")
elif (age >=2) and (age < 4):
print("toddler")
elif (age >=4) and (age < 13):
print("kid")
elif (age >=13) and (age < 20):
print("teenager")
elif (age >=20) and (age < 65):
print("adult")
else:
print("elder")
π§ Code Explanation:
This Python code demonstrates the use of an if-elif-else
chain to determine a person's stage of life based on their age. Here's how it works:
-
Define the variable:
The variable
age
is assigned a value representing a person's age. In this example, it is set to50
. -
Use an
if-elif-else
chain:The program evaluates the value of
age
against multiple conditions to determine the person's stage of life:if (age < 2)
: If the age is less than 2, the person is classified as a "baby."elif (age >= 2) and (age < 4)
: If the age is between 2 and 4 (inclusive of 2 but less than 4), the person is classified as a "toddler."elif (age >= 4) and (age < 13)
: If the age is between 4 and 13 (inclusive of 4 but less than 13), the person is classified as a "kid."elif (age >= 13) and (age < 20)
: If the age is between 13 and 20 (inclusive of 13 but less than 20), the person is classified as a "teenager."elif (age >= 20) and (age < 65)
: If the age is between 20 and 65 (inclusive of 20 but less than 65), the person is classified as an "adult."else
: If none of the above conditions are true (i.e., the age is 65 or older), the person is classified as an "elder."
-
Output the result:
The program outputs the stage of life corresponding to the value of
age
.
This code demonstrates how to use an if-elif-else
chain to evaluate multiple conditions and classify data based on specific criteria.
π Output:
adult
π Related Exercises from Chapter 5:
- ➤ Exercise 5-1 | Conditional Tests
- ➤ Exercise 5-2 | More Conditional Tests
- ➤ Exercise 5-3 | Alien Colors #1
- ➤ Exercise 5-4 | Alien Colors #2
- ➤ Exercise 5-5 | Alien Colors #3
- ➤ Exercise 5-6 | Stages of Life
- ➤ Exercise 5-7 | Favorite Fruit
- ➤ Exercise 5-8 | Hello Admin
- ➤ Exercise 5-9 | No Users
- ➤ Exercise 5-10 | Checking Usernames|
- ➤ Exercise 5-11 | Ordinal Numbers
- ➤ Exercise 5-12 | Styling if statements
- ➤ Exercise 5-13 | Your Ideas
π Connect With Us:
“Programs must be written for people to read, and only incidentally for machines to execute.”
— Harold Abelson
π Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment