π 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:
-
Create a guest list:
The variable
guest
is assigned a list containing the names of the guests:["Iron man", "Captain", "Hulk"]
. -
Calculate the total number of guests:
The
len()
function is used to calculate the number of elements in theguest
list. The result is stored in the variabletotal_guest
. -
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. -
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:
- ➤ Exercise 3-1 -> Names
- ➤ Exercise 3-2 -> Greetings
- ➤ Exercise 3-3 -> Your Own List
- ➤ Exercise 3-4 -> Guest List
- ➤ Exercise 3-5 -> Changing Guest List
- ➤ Exercise 3-6 -> More Guests
- ➤ Exercise 3-7 -> Shrinking Guest List
- ➤ Exercise 3-8 -> Seeing the World
- ➤ Exercise 3-9 -> Dinner Guests
- ➤ Exercise 3-10 -> Every Function
π 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
π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