Exercise 5-6 Solution Python Crash Course Chapter 5: if Statements

 

📘 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:

  1. Define the variable:

    The variable age is assigned a value representing a person's age. In this example, it is set to 50.

  2. 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."
  3. 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:


🔗 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

Previous Post Next Post

Contact Form