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

 

πŸ“˜ 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:

  1. Create a list of favorite fruits:

    The variable fav_friuits is assigned a list containing five favorite fruits: ["apple", "banana", "mango", "grapes", "papaya"].

  2. Check for specific fruits:

    A series of independent if statements are used to check whether specific fruits are present in the fav_friuits list. Each if statement evaluates whether a fruit is in the list using the in operator.

  3. Perform actions based on the condition:

    If the condition in an if statement evaluates to True, a message is displayed indicating that the fruit is liked. If the condition evaluates to False, no action is taken, and the program moves to the next if statement.

  4. 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:


πŸ”— 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

“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