π Python Crash Course – Chapter 5: if Statements – Exercise 5-4 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 5-4
Task:
Choose a color for an alien as you did in Exercise 5-3, and
write an if-else chain .
• If the alien’s color is green, print a statement that the player just earned
5 points for shooting the alien .
• If the alien’s color isn’t green, print a statement that the player just earned
10 points .
• Write one version of this program that runs the if block and another that
runs the else block
✅ Solution Code:
alien_color = "green"
if alien_color == "green":
print("you earned 5 points")
else:
print("you earned 10 points ")
print("------------------------------------------------------")
alien_color = "yellow"
if alien_color == "green":
print("you earned 5 points")
else:
print("you earned 10 points ")
print("------------------------------------------------------")
π§ Code Explanation:
This Python code demonstrates the use of an if-else
chain to handle two possible 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. In the first case, it is set to"green"
, and in the second case, it is set to"yellow"
. -
Use an
if-else
chain:An
if
statement checks whether the value ofalien_color
is"green"
. If the condition is true, a message is displayed indicating that the player earned 5 points. If the condition is false, theelse
block is executed, displaying a message that the player earned 10 points. -
Demonstrate both cases:
The program includes two versions of the test: one where the condition passes (when
alien_color
is"green"
) and another where the condition fails (whenalien_color
is"yellow"
).
This code demonstrates how to use an if-else
chain to handle two mutually exclusive conditions in Python.
π Output:
you earned 5 points
------------------------------------------------------
you earned 10 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