Learn Intermediate Python 3 - Event Coordinator Project

Project: Learn Intermediate Python 3 - Event Coordinator

Hi, Codecademy members! I’d like to post the solution I came with for the current project and to make a question about the Step 3.

guests = {}
def read_guestlist(file_name):
  text_file = open(file_name,'r')
  while True:
    line_data = text_file.readline().strip().split(",")
    # Step 1
    n = yield line_data
    # Step 2
    if n != None:
      line_data = n.split(',')
      name = line_data[0]
      age = int(line_data[1])
      guests[name] = age
      continue
    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

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

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

# Step 3
for i in range(4):
  print(next(guest_list))

# Step 4
guests_over_21 = (guest for guest in guests if guests[guest] >= 21)
print(list(guests_over_21))

# Step 5
def table_1():
  food = 'Chicken'
  table = 1
  for i in range(5):
    i += 1
    yield f'Food: {food}', 'Table 1', f'{i}'

def table_2():
  food = 'Beef'
  table = 2
  for i in range(5):
    i += 1
    yield f'Food: {food}', 'Table 2', f'{i}'

def table_3():
  food = 'Fish'
  table = 3
  for i in range(5):
    i += 1
    yield f'Food: {food}', 'Table 3', f'{i}'

def combined_tables():
  yield from table_1()
  yield from table_2()
  yield from table_3()

combined_tables = combined_tables()

# Step 6
def assign_tables(guests):
  for i in guests.keys():
    yield (i, next(combined_tables))

assign_tables = assign_tables(guests)
for i in assign_tables:
  print(i)

Whenever I run the code on Step 3 I get as output the following (Jane is not included):

[‘Tim’, ‘22’]
[‘Tonya’, ‘45’]
[‘Mary’, ‘12’]
[‘Ann’, ‘32’]
[‘Beth’, ‘20’]
[‘Sam’, ‘5’]
[‘Manny’, ‘76’]
[‘Kenton’, ‘15’]
[‘Kenny’, ‘27’]
[‘Dixie’, ‘46’]
[‘Julian’, ‘4’]
[‘Edward’, ‘71’]
[‘Rose’, ‘65’]
[’’]

But if I run the code on Step 4 I can see as output (Jane is included):

[‘Tim’, ‘Tonya’, ‘Ann’, ‘Manny’, ‘Kenny’, ‘Jane’, ‘Mallory’, ‘Edward’, ‘Rose’]

I reviewed the lecture several times and I’m having missing parts obviously regarding that behaviour. I’m not assuming my code is right, just wondering why is that output produced (Also referring to the None value at the end).

Thank you very much in advance for your time and help during this learning process, I’m having a lot of fun. Really appreciated.

Cristian

At the moment it looks like you risk missing a user, on every iteration you read a line from the file and allow for use of input from .send. You’d likely want to set this up so only one of these is possible in each iteration or you risk missing one or the other.

Chances are step 4 just so happens to skip something it was meant to skip anyway, I’ve not tested it but that’d be my first guess.

The empty bit is probably because .readline() from a file that has reached the end still works, it just returns an empty string. You’ll want some way to stop iterating through the file without yielding anything once you hit the end (it might even be wise to close the generator object at that point with return).

When you call guest_list.send(“Jane,35”) in step2 it actually runs through the main function until it gets to yield again. So it’s doing the right things (since it shows up in step 4). When you call print(next(guest_list)) in step3, it now moves past Jane and onto the next value. If you want it to print, just add modify your original step2 with a print statement to print(guest_list.send("Jane, 35).

This problem drove me nuts too.