Event coordinator step 4

I am having trouble with the event coordinator project from Learn Intermediate Python 3. I successfully completed steps 1-3 but on step 4 my over_21 generator expression does not have ‘Jane’ on the guest list:

Here are the instructions:
Now that we have all our guests, we want to see which guests are aged 21 and over so that we can inform the bartending vendor.
Define a generator expression that will use the guests dictionary to retrieve a generator of names of all guests over 21 years of age.
We should see our newly added guest, Jane on this list as well!

Code is below:

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

guest_list = read_guestlist('guest_list.txt')

for i in range(10):
  print(next(guest_list))

guest_list.send(['Jane', '35'])

for guest in guest_list:
  print(guest)

over_21 = (name for name in guests if int(guests[name]) >= 21)
for name in over_21:
  print(name)

At the moment you use yield like a statement instead of an expression so you have no route to handle data sent into the generator. You might want to recap the lessons on the .send method as you’ll need assignment and probably some further logical steps to deal with this.

2 Likes

Thank you! I’ve had the same problem. Solved it after reading your comment. Posting below my way of solving the project with Jane appearing inside the guest list.

The only thing I don’t understand is why by printing every guest in dictionary before task 4 I get only name without guests’ ages…

for i in guests:
  print(i)
guests = {} def read_guestlist(file_name): text_file = open(file_name,'r') while True: line_data = text_file.readline().strip().split(",") 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 val = yield name, age if val is not None: val = val.split(",") name = val[0] age = int(val[1]) guests[name] = age yield name, age guestlist = read_guestlist('guest_list.txt') #task 3; #Adding Jane,35 for i in range(10): print(next(guestlist)) guestlist.send("Jane, 35") print(next(guestlist)) print(next(guestlist)) print(next(guestlist)) print(next(guestlist)) #question for i in guests: print(i) #task 4 over_21 = (key for key, val in guests.items() if val > 21) for guest_name in over_21: print(guest_name) #task 5 def chicken(): food = 'Chicken' table = 1 for i in range(5): seat = i + 1 yield f'menu: {food}', f'table: {table}', f'seat: {seat}' def beef(): food = 'Beef' table = 2 for i in range(5): seat = i + 1 yield f'menu: {food}', f'table: {table}', f'seat: {seat}' def fish(): food = 'Fish' table = 3 for i in range(5): seat = i + 1 yield f'menu: {food}', f'table: {table}', f'seat: {seat}' #task 6 def meal_assigner(guests, generator1, generator2, generator3): names = list(guests.keys()) for i in range(5): yield (names[i], next(generator1)) for i in range(5): i += 5 yield (names[i], next(generator2)) for i in range(5): i += 10 yield (names[i], next(generator3)) meal_plans = meal_assigner(guests, chicken(), fish(), beef()) for i in meal_plans: print(i)
1 Like

Ah, kudos for finding a solution :+1:. The dictionary bit is just because the standard iteration through a dictionary is solely through the keys of that dictionary (the guest names in this case). If you wanted something else from the dictionary make sure to use the relevant method for it.

1 Like

Ah, so it was working as intended! Understood, thank you again! :grinning:

1 Like