Learn Intermediate Python 3 - Event Coordinator Project Help

Hi all,

I am having trouble on step 1 and 2 of this project: Event Coordinator Project

I’ve found a few different ways to return the first ten names, but have not been successful in using the send() method for generator objects to add “Jane,25” to the generator function. Here is my code so far:

guests = {}
def read_guestlist(file_name):
  text_file = open(file_name,'r')
  while True:
    line_data = text_file.readline().strip().split(",")
    # Step 1: they seem to be asking for the full data included in each line wholey, not just name. I couldn't figure out how to yield just the name AND potentially use the send() method to add a name and age from that same yield call.
    n = yield line_data
    if len(line_data) < 2:
    # If no more lines, close file
    name = line_data[0]
    age = int(line_data[1])
    guests[name] = age

names = read_guestlist('guest_list.txt')
for i in range(10):

# Step 2

# Used this to check if the send method worked.

I’ve gone back and reviewed the whole module on generators, and nothing has “clicked” for me to determine what I’m doing wrong. As I’ve understood it so far, once the yield statement is reached, the function’s execution is suspended with local variables stored, so by executing the yield statement where I did, to my understanding that will allow the send() method to insert “Jane,25” at that point and assign her key/value pair to the guests{} dictionary, right? Do I need some sort of control statement checking if n is not None? I can’t think of a reason to include that.

Also, in step one, they ask us to return the names of the first ten guests. I couldn’t for the life of me figure out how to do that. I settled with use a for loop and range function to iterate through the generator object and print out the pairs. They were return in brackets, not parentheses like I expected.

The output of the above code is:
[‘Tim’, ‘22’]
[‘Tonya’, ‘45’]
[‘Mary’, ‘12’]
[‘Ann’, ‘32’]
[‘Beth’, ‘20’]
[‘Sam’, ‘5’]
[‘Manny’, ‘76’]
[‘Kenton’, ‘15’]
[‘Kenny’, ‘27’]
[‘Dixie’, ‘46’]
{‘Tim’: 22, ‘Tonya’: 45, ‘Mary’: 12, ‘Ann’: 32, ‘Beth’: 20, ‘Sam’: 5, ‘Manny’: 76, ‘Kenton’: 15, ‘Kenny’: 27, ‘Dixie’: 46}

The send method did not add Jane to the dictionary.

This is my first time asking a question on the forums. I’m trying my best to ask “good” questions, so any feedback on my format of asking would be helpful too. Thank you!


For step 1, you can simply yield just the guest name instead of the full line_data: yield name.

With the send method, the solution is more complicated. The yield will return the value sent to the generator. So you need to process n like any other line_data. I think it’s necessary to use nested loops.

And the send method works like next, returning the parsed value of the send argument. So print the result of send too.

You can see my solution for the first 4 steps below.

guests = {}
def read_guestlist(file_name):
  text_file = open(file_name,'r')
  while True:
    line = text_file.readline().strip()
    if not line:
    # If no more lines, close file

    while line:
      line_data = line.split(",")
      name = line_data[0]
      age = int(line_data[1])
      guests[name] = age

      line = yield name

gen = read_guestlist('guest_list.txt')

for _ in range(10):


for name in gen:

print(list(x for x in guests if guests[x] > 21))

Hey teekay! Welcome to the forums!

Any question is a good question, and your question was well formed. The only thing I might change would be to make sure that your code output is formatted in a code block, very much like the code you posted. Honestly, its no big deal, it just makes it easier to read is all.

You are correct, you do need a conditional that verifies that the data sent to yield via send() is not None. This will allow you to process the value you sent in however you see fit. Without that conditional, the value you send in gets stored in your n variable, but nothing is ever done with it (this is why it isn’t appearing in your dictionary). Hopefully that makes sense. The send() method is a mind-bender for sure.

In case you find it useful, here is the code I ended up using for the first couple of steps:

# Intermediate Python | Generators (Event Coordinator) def read_guestlist(file_name): with open(file_name) as f: entries = f.read().splitlines() while entries: line_data = entries.pop().split(',') name, age = line_data[0], int(line_data[1]) guests[name] = age entry = yield name if entry is not None: entries.append(entry) guests = {} guestlist = read_guestlist('guest_list.txt') for _ in range(10): print(next(guestlist)) print(guestlist.send('Jane,35')) for guest in guestlist: print(guest)

Cheers and God bless! :pray: