Help with Generators (Intermediate Python 3 Project: Event Coordinator)

First of all let me say that this topic of generators (and in particular, making a generator compatible with the send() method) was a bit a bit shaky for me. I fumbled my way through the lesson, but didn’t really get it.

So now I’m on this project: https://www.codecademy.com/courses/learn-intermediate-python-3/projects/int-python-event-coordinator

I was able to do the first checkpoint fairly easily, but when I got to the second checkpoint… I looked back at the lesson information for the send() method and wrote something resembling what was in that lesson, but it’s not working properly. Here’s the code I have:

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
    guest = line_data[0]
    new_guest = yield guest
    if new_guest is not None:
      guest = new_guest
    age = int(line_data[1])
    guests[guest] = age

glist = read_guestlist("guest_list.txt")
count = 0
for guest in glist:
  count += 1
  if count == 10:
    print(glist.send("Jane"))
  print(guest)
  

This code is simply printing every guest- when it hits line 10, it doesn’t send “Jane” as I am expecting it to do, but rather still prints “Mallory” and then continues with the rest of the list. Even when I tried not using the loop and instead wrote out 10 print(next(glist)) statements followed by a print(glist.send(“Jane”)) statement, Jane was not printed. All of this tells me that I’m clearly doing something wrong in how I defined the generator, but since I don’t really understand how to make the send() method work, I’m stuck.

Any help would be greatly appreciated!Preformatted text

I put the print(guest) before the if-statement, before count += 1 in mine.

Hmm I just tried this but it didn’t work. It’s still not printing Jane

I should say that I managed to jury rig a solution by putting a print statement in the if new_guest is not None block in the generator function. But that didn’t really seem like a good solution

I guess you could put all the stuff that deals with line_data inside if-blocks that only run if there’s no new_guest so that reading another line from the file doesn’t overwrite guest (which may have been taken from new_guest in the previous iteration)
but that nested if wouldn’t be pretty.

guests = {}
def read_guestlist(file_name):
  text_file = open(file_name,'r')
  new_guest = None
  age = 0
  while True:
    if new_guest is None:
        line_data = text_file.readline().strip().split(",")
      if len(line_data) < 2:
        # If no more lines, close file
        text_file.close()
        break
      guest = line_data[0]
      age = int(line_data[1])
      # end of if-block "if new_guest is None"

    new_guest = yield guest
    if new_guest is not None:
      guest = new_guest
      # need code to get age for new_guest here ?
    #age = int(line_data[1])  # move to earlier in the code
    guests[guest] = age

# end of function

I had the loop in a different order than this in my version.

Hi!
@janbazant1107978602 I got inspired by your code to finish this project.
To anyone who will have any problems completing it below you can find my solution (including comments which refer to different project tasks):

guests = {}
# Task #1
def read_guestlist(file_name):
  text_file = open(file_name,'r')
  new_guest = None
  age = 0
  while True:
    if new_guest is None:
      line_data = text_file.readline().strip().split(",")
    if line_data is not None:
      if len(line_data) < 2:
      # If no more lines, close file
        text_file.close()
        break
      guest = line_data[0]
      age = int(line_data[1])
    new_guest = yield guest
    if new_guest is not None:
      new_guest_split = new_guest.split(",")
      guest = new_guest_split[0]
      age = int(new_guest_split[1])
    guests[guest] = age

# Task #1
print("======= GUEST LIST =======")
guest_generator = read_guestlist("guest_list.txt")
guest_counter = 0
for guest in guest_generator:
  if guest_counter < 10:
    print(guest)
    guest_counter += 1
  else:
    break

# Task #2
guest_generator.send("Jane,35")

# Task #3
for guest in guest_generator:
  print(guest)

# Task #4
print()
print("===== BARTENDER LIST =====")
bartender_generator = (guest for guest, age in guests.items() if age >= 21)
for guest in bartender_generator:
  print(guest)

# Task #5
print()
print("=== TABLES SEATS ===")
def table_one():
  for seat in range(1, 6):
    seat_info = "Seat {}".format(seat)
    yield ("Chicken", "Table 1", seat_info)

def table_two():
  for seat in range(1, 6):
    seat_info = "Seat {}".format(seat)
    yield ("Beef", "Table 2", seat_info)
  
def table_three():
  for seat in range(1, 6):
    seat_info = "Seat {}".format(seat)
    yield ("Chicken", "Table 3", seat_info)

# Task #6
import itertools
tables = itertools.chain(table_one(), table_two(), table_three())
guests_seats = ((guest, next(tables)) for guest in guests.keys())
for seat in guests_seats:
  print(seat)

4 Likes

Thanks for sharing this, appreciated. It just strikes me as lazy that Codecademy charge people however many hundred dollars a year for pro membership and then couldn’t be bothered to provide solutions for projects like this.

1 Like

I have been struggling immensely. There is literally no help available from inside the Codecademy portal. And the discussion here answers to some of the issues, but still falls short.

Thanks for the help – I am new to Python and am grateful for this discussion.

To print Jane on Step 2:

  • Changed the def read_guestlist generator to the try/except model shown in the .close() lesson
    except GeneratorExit:
    print(“Jane”)
    break

When I use the .send method, the generator object isn’t updated to include Jane, but the dictionary, guests, is. And I think that makes sense. The generator isn’t trying to update the original file, it’s just updating the dictionary.