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

 

📘 Python Crash Course – Chapter 5: if Statements – Exercise 5-5 Solution

Welcome to another solution from Python Crash Course by Eric Matthes.


📝 Exercise 5-5

Task:
Turn your if-else chain from Exercise 5-4 into an if-elif else chain .
• If the alien is green, print a message that the player earned 5 points .
• If the alien is yellow, print a message that the player earned 10 points .
• If the alien is red, print a message that the player earned 15 points .
• Write three versions of this program, making sure each message is printed for the appropriate color alien

✅ Solution Code:


alien_color = "green"

if alien_color == "green":
    print("you earned 5 points")

elif alien_color=="yellow":
    print("you earned 10 points")
else:
    print("you earned 15 points ")

print("---------------------------------------------------------------")

alien_color = "yellow"

if alien_color == "green":
    print("you earned 5 points")

elif alien_color=="yellow":
    print("you earned 10 points")
else:
    print("you earned 15 points ")

print("---------------------------------------------------------------")

alien_color = "red"

if alien_color == "green":
    print("you earned 5 points")

elif alien_color=="yellow":
    print("you earned 10 points")
else:
    print("you earned 15 points ")

print("---------------------------------------------------------------")

🧠 Code Explanation:

This Python code demonstrates the use of an if-elif-else chain to handle multiple conditions based on the value of a variable. Here's how it works:

  1. Define the variable:

    The variable alien_color is assigned a value representing the color of an alien. The program tests three different values: "green", "yellow", and "red".

  2. Use an if-elif-else chain:

    The if statement checks if the value of alien_color is "green". If true, a message is displayed indicating that the player earned 5 points. If the condition is false, the program moves to the elif block, which checks if the value is "yellow". If this condition is true, a message is displayed indicating that the player earned 10 points. If neither condition is true, the else block is executed, displaying a message that the player earned 15 points.

  3. Test all cases:

    The program includes three versions of the test, each with a different value for alien_color ("green", "yellow", and "red"). This ensures that all branches of the if-elif-else chain are tested.

This code demonstrates how to use an if-elif-else chain to handle multiple mutually exclusive conditions in Python.

🔍 Output:


you earned 5 points
---------------------------------------------------------------
you earned 10 points
---------------------------------------------------------------
you earned 15 points
---------------------------------------------------------------

📚 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