π 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:
-
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"
. -
Use an
if-elif-else
chain:The
if
statement checks if the value ofalien_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 theelif
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, theelse
block is executed, displaying a message that the player earned 15 points. -
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 theif-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:
- ➤ 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