π Python Crash Course – Chapter 5: if Statements – Exercise 5-7 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 5-7
Task:
Make a list of your favorite fruits, and then write a series of
independent if statements that check for certain fruits in your list .
• Make a list of your three favorite fruits and call it favorite_fruits .
• Write five if statements . Each should check whether a certain kind of fruit
is in your list . If the fruit is in your list, the if block should print a statement,
such as You really like bananas!
✅ Solution Code:
fav_friuits = ["apple","banana","mango","grapes","papaya"]
fruit ="grapes"
if fruit in fav_friuits:
print(f"I really like {fruit}")
print("-------------------------------------------------------")
fruit ="mango"
if fruit in fav_friuits:
print(f"I really like {fruit}")
print("-------------------------------------------------------")
fruit ="salads"
if fruit in fav_friuits:
print(f"I really like {fruit}")
print("-------------------------------------------------------")
fruit ="tomato"
if fruit in fav_friuits:
print(f"I really like {fruit}")
print("-------------------------------------------------------")
fruit ="apple"
if fruit in fav_friuits:
print(f"I really like {fruit}")
print("-------------------------------------------------------")
fruit ="soyabean"
if fruit in fav_friuits:
print(f"I really like {fruit}")
print("-------------------------------------------------------")
fruit ="papaya"
if fruit in fav_friuits:
print(f"I really like {fruit}")
print("-------------------------------------------------------")
π§ Code Explanation:
This Python code demonstrates how to use a series of independent if
statements to check for specific items in a list of favorite fruits. Here's how it works:
-
Create a list of favorite fruits:
The variable
fav_friuits
is assigned a list containing five favorite fruits:["apple", "banana", "mango", "grapes", "papaya"]
. -
Check for specific fruits:
A series of independent
if
statements are used to check whether specific fruits are present in thefav_friuits
list. Eachif
statement evaluates whether a fruit is in the list using thein
operator. -
Perform actions based on the condition:
If the condition in an
if
statement evaluates toTrue
, a message is displayed indicating that the fruit is liked. If the condition evaluates toFalse
, no action is taken, and the program moves to the nextif
statement. -
Iterate through multiple fruits:
The program checks for various fruits, including those in the list (e.g., "grapes", "mango", "apple", "papaya") and those not in the list (e.g., "salads", "tomato", "soyabean").
This code demonstrates how to use independent if
statements to evaluate multiple conditions and perform actions based on the presence of specific items in a list.
π Output:
I really like grapes
-------------------------------------------------------
I really like mango
-------------------------------------------------------
-------------------------------------------------------
-------------------------------------------------------
I really like apple
-------------------------------------------------------
-------------------------------------------------------
I really like papaya
-------------------------------------------------------
π 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