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
      text_file.close()
      break
    name = line_data[0]
    age = int(line_data[1])
    guests[name] = age
    

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

# Step 2
names.send("Jane,35")

# Used this to check if the send method worked.
print(guests)

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!

Hello!

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.

Solution
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
      text_file.close()
      break

    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):
  print(next(gen))

print(gen.send('Jane,35'))

for name in gen:
  print(name)

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