π Python Crash Course – Chapter 3: Introducing Lists – Exercise 3-7 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 3-7
Task: You just found out that your new dinner table won’t arrive in time for the dinner, and you have space for only two guests .
• Start with your program from Exercise 3-6 . Add a new line that prints a
message saying that you can invite only two people for dinner .
• Use pop() to remove guests from your list one at a time until only two
names remain in your list . Each time you pop a name from your list, print
a message to that person letting them know you’re sorry you can’t invite
them to dinner .
• Print a message to each of the two people still on your list, letting them
know they’re still invited .
• Use del to remove the last two names from your list, so you have an empty
list . Print your list to make sure you actually have an empty list at the end
of your program
✅ Solution Code:
# We use exercise 3.6
guest = ["Iron man" , "Captain" , "Hulk"]
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')
print("Hello Everyone\nWe are happy to inform you that we have Three more guests")
guest.insert(0,"Hwakeye")
guest.insert(2,"Natasha")
guest.append("Nick Fury")
print("-----------------------------------------------------------------------")
#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')
print(f'Hello {guest[3]} Thanks for attending My Party')
print(f'Hello {guest[4]} Thanks for attending My Party')
print(f'Hello {guest[5]} Thanks for attending My Party')
print("-----------------------------------------------------------------------")
# now we remove guests
print("Removing Guests")
guest_remove = guest.pop()
print(f'Sorry {guest_remove} I cant Invite you in The party')
guest_remove = guest.pop()
print(f'Sorry {guest_remove} I cant Invite you in The party')
guest_remove = guest.pop()
print(f'Sorry {guest_remove} I cant Invite you in The party')
guest_remove = guest.pop()
print(f'Sorry {guest_remove} I cant Invite you in The party')
print("-----------------------------------------------------------------------")
print(f"Remaining Guests Are : {guest[0]} , {guest[1]}")
print(f'Hello {guest[0]} Thanks for attending My Party')
print(f'Hello {guest[1]} Thanks for attending My Party')
print("-----------------------------------------------------------------------")
# Removing the Two Guests Also
del guest[:] # remove all remaining Guests
# Important Note - if we remove members from starting or middle then indexing of all other elements will also change means become (index - 1)
#now guest list is empty
print(guest)
π§ Code Explanation:
This Python code demonstrates how to manage a guest list for a party, handle changes due to limited space, and dynamically update invitations. Here's how it works:
-
Create an initial guest list:
The variable
guest
is assigned a list containing the names of the initial guests:["Iron man", "Captain", "Hulk"]
. -
Send initial invitations:
The
print()
function is used with f-strings to send personalized invitations to each guest in the original list. -
Add more guests:
guest.insert(0, "Hwakeye")
: Adds "Hwakeye" to the beginning of the list.guest.insert(2, "Natasha")
: Adds "Natasha" to the middle of the list (at index 2).guest.append("Nick Fury")
: Adds "Nick Fury" to the end of the list.
A message is printed to inform everyone about the additional guests.
-
Send updated invitations:
The
print()
function is used again with f-strings to send personalized invitations to each guest in the updated list. -
Remove guests due to limited space:
A message is printed stating that only two guests can be invited. The
pop()
method is used to remove guests one by one from the end of the list, and a message is printed for each removed guest. -
Send final invitations:
The remaining two guests are displayed, and personalized invitations are sent to them.
-
Clear the guest list:
The
del
statement is used to remove all remaining guests from the list, leaving it empty. The empty list is printed to confirm that it has been cleared.
π Output:
Hello Iron man Thanks for attending My Party
Hello Captain Thanks for attending My Party
Hello Hulk Thanks for attending My Party
Hello Everyone
We are happy to inform you that we have Three more guests
-----------------------------------------------------------------------
New Invitation
Hello Hwakeye Thanks for attending My Party
Hello Iron man Thanks for attending My Party
Hello Natasha Thanks for attending My Party
Hello Captain Thanks for attending My Party
Hello Hulk Thanks for attending My Party
Hello Nick Fury Thanks for attending My Party
-----------------------------------------------------------------------
Removing Guests
Sorry Nick Fury I cant Invite you in The party
Sorry Hulk I cant Invite you in The party
Sorry Captain I cant Invite you in The party
Sorry Natasha I cant Invite you in The party
-----------------------------------------------------------------------
Remaining Guests Are : Hwakeye , Iron man
Hello Hwakeye Thanks for attending My Party
Hello Iron man 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