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

 

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

  1. Create an initial guest list:

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

  2. Send initial invitations:

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

  3. 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.

  4. Send updated invitations:

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

  5. 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.

  6. Send final invitations:

    The remaining two guests are displayed, and personalized invitations are sent to them.

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


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

“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