Intermediate Python 3 - Events Coordinator

Hello,

I’m looking for help on the Intermediate Python events coordinator project, specifically step 2. I don’t understand why I can see “Jane, 35” when next is called from the guest list? My code is below.

https://www.codecademy.com/courses/learn-intermediate-python-3/projects/int-python-event-coordinator

# insert index in generator to count to 10?

guestlist = read_guestlist('guest_list.txt')

for i in range(10):

print(next(guestlist))

print(guestlist.send("Jane,35"))

print(next(guestlist))

print(next(guestlist))

print(next(guestlist))

print(next(guestlist))

Thanks in advance.

I am also stuck on this. Can print all the names in the list, but adding new guest using send is throwing StopIteration error. Not a lot of help with these questions and some are very poorly phrased.

@textcoder73315 Did you manage to figure this out? Was wondering if the issue is with the yield statement?

I did eventually get to the bottom of this. I believe my issue was in step 1, in the thrid paragraph it states:

‘Modify this function to be a generator function that will yield each read line so that each guest name is yielded each time for the generator.’

I originally skipped over this unintentionally.

It now looks like this,

guests = {}
def read_guestlist(file_name):
  text_file = open(file_name,'r')
  value = None
  while True:
    if value is not None:
      line_data = value.strip().split(",")
    else:
      line_data = text_file.readline().strip().split(",")
    #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
    value = yield name

This solves the issue I was having and is more or less plain sailing from here on out.

1 Like

Thank you very much. Thought there was a problem with yield but couldn’t figure it out. I had
line_data = yield name
Much appreciated @textcoder73315