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

 

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

  1. Create a guest list:

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

  2. Send the old invitations:

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

  3. Handle a guest who can't attend:

    A message is printed stating that the first guest (guest[0], "Iron man") cannot attend the party.

  4. Replace the unavailable guest:

    The first guest in the list is replaced with a new guest, "Thanos," using the assignment guest[0] = "Thanos".

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


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