Exercise 3-9 Solution Python Crash Course Chapter 3: Introducing Lists

 

📘 Python Crash Course – Chapter 3: Introducing Lists – Exercise 3-9 Solution

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


📝 Exercise 3-9

Task: Working with one of the programs from Exercises 3-4 through 3-7 , use len() to print a message indicating the number of people you are inviting to dinner

✅ Solution Code:


# we use exercise 3.4

guest = ["Iron man" , "Captain" , "Hulk"]

# Total Person
total_guest = len(guest)
print(f"Total Invited Guest : {total_guest}")

print(f'Hello {guest[0]} Thanks for attending My Party')
print(f'Hello {guest[1]} Thanks for attending My Party')
print(f'Hello {guest[2]} Thanks for attending My Party')

🧠 Code Explanation:

This Python code demonstrates how to calculate the number of guests invited to a dinner party using the len() function. Here's how it works:

  1. Create a guest list:

    The variable guest is assigned a list containing the names of the guests: ["Iron man", "Captain", "Hulk"].

  2. Calculate the total number of guests:

    The len() function is used to calculate the number of elements in the guest list. The result is stored in the variable total_guest.

  3. Print the total number of guests:

    The print() function is used with an f-string to display a message indicating the total number of guests invited.

  4. Send personalized invitations:

    The print() function is used with f-strings to send personalized invitations to each guest in the list.

🔍 Output:


Total Invited Guest : 3
Hello Iron man Thanks for attending My Party
Hello Captain Thanks for attending My Party
Hello Hulk Thanks for attending My Party

📚 Related Exercises from Chapter 3:


🔗 Connect With Us:

“The only way to learn a new programming language is by writing programs in it.”
– Dennis Ritchie

📌 Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips #introducingLists

Previous Post Next Post

Contact Form