π Python Crash Course – Chapter 3: Introducing Lists – Exercise 3-5 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 3-5
Task: You just heard that one of your guests can’t make the
dinner, so you need to send out a new set of invitations . You’ll have to think of
someone else to invite.
• Start with your program from Exercise 3-4 . Add a print statement at the
end of your program stating the name of the guest who can’t make it .
• Modify your list, replacing the name of the guest who can’t make it with
the name of the new person you are inviting .
• Print a second set of invitation messages, one for each person who is still
in your list
✅ Solution Code:
guest = ["Iron man" , "Captain" , "Hulk"]
# Old invitation
print("Old Invitation")
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')
# Person who is not attending
print(f'{guest[0]} is not addending the party')
# replacing the guest with new one
guest[0] = "Thanos"
# Writing New Invitation
print("New Invitation")
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')
e)
π§ Code Explanation:
This Python code demonstrates how to manage a guest list for a party, handle changes to the list, and send updated invitations. Here's how it works:
-
Create a guest list:
The variable
guest
is assigned a list containing the names of the initial guests:["Iron man", "Captain", "Hulk"]
. -
Send the old invitations:
The
print()
function is used with f-strings to send personalized invitations to each guest in the original list. -
Handle a guest who can't attend:
A message is printed stating that the first guest (
guest[0]
, "Iron man") cannot attend the party. -
Replace the unavailable guest:
The first guest in the list is replaced with a new guest, "Thanos," using the assignment
guest[0] = "Thanos"
. -
Send the new invitations:
The
print()
function is used again with f-strings to send updated invitations to the modified guest list.
π Output:
Old Invitation
Hello Iron man Thanks for attending My Party
Hello Captain Thanks for attending My Party
Hello Hulk Thanks for attending My Party
Iron man is not addending the party
New Invitation
Hello Thanos 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