0

I was required to create a list that includes at least three people I’d like to invite to dinner. Then use this list to print a message to each person, inviting them to dinner.

Code:

My_Guests = ['Charlie','George','Bennjamin','Winslow']
message ="You are invited to a dinner on the 5th August"
print(f'Guest invites:\n\tDear Mr {My_Guests[0]}, {message}\n\tDear Mr {My_Guests[1]}, {message}\n\tDear Mr {My_Guests[2]}, {message}\n\tDear Mr {My_Guests[3]}, {message}')

Output:

Guest invites:
    Dear Mr Charlie,You are invited to a dinner on the 5th August
    Dear Mr George,You are invited to a dinner on the 5th August
    Dear Mr Benjamin,You are invited to a dinner on the 5th August
    Dear Mr Winslow,You are invited to a dinner on the 5th August

Is there a simpler code to solving this problem?

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Does this answer your question? [How to print the list in 'for' loop using .format() in Python?](https://stackoverflow.com/questions/51795644/how-to-print-the-list-in-for-loop-using-format-in-python) – doneforaiur Aug 09 '23 at 14:53
  • 2
    Loops. Think in terms of *data* not copy-pasting code. `for guest in guests:` – tadman Aug 09 '23 at 14:54

1 Answers1

0

You can do it like so:

my_guests = ["Charlie", "George", "Bennjamin", "Winslow"]
message = "you are invited to a dinner on the 5th August."

print("Guest invites:")
for guest in my_guests:
    print(f"\tDear Mr {guest}, {message}")
chubercik
  • 534
  • 1
  • 5
  • 13