Event Coordinator Project

Hello, I’m having some problems with this project: https://www.codecademy.com/courses/learn-intermediate-python-3/projects/int-python-event-coordinator.

I can’t seem to get past step 1. Here is my code:

guests = {}
def read_guestlist(file_name):
text_file = open(‘guest_list.txt’,‘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
# Modifying the function to be a generator
yield name

for guest in read_guestlist(guests):
print(guest)

I cannot seem to figure out how to only print the first 10 guests as the instructions say. Any help is appreciated.

1 Like

I got stuck in this project but later, step 5. I’ve written step 1 so:

guestlist = read_guestlist('guest_list.txt')

# for guest in guestlist:
for i in range(10):
    print(next(guestlist))

Thank you very much, that worked. Now let’s see if I can get through the rest of them :slight_smile:

Now I cannot seem to get step 2 done. 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(",")
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 - creating generator
yield name, age

Step 1

guestlist = read_guestlist(‘guest_list.txt’)

Shows tuples of the names and ages of the first 10 guests

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

Step 2

first I tried:

next(guestlist)
guestlist.send(‘Jane, 35’)

or

guestlist.send(None)
guestlist.send(‘Jane, 35’)

Neither of these solutions seem to add Jane to the list.

Does anyone know where I’m going wrong. Any help is appreciated. Thanks!

Hi, I found the solution to your problem thanks to another user’s help (thanks @raultozetto019835826 )

2 Likes

Hi, i solved the step 2 so:

Function:

def read_guestlist(file_name):
    text_file = open(file_name,'r')
    val = None
    while True:
        if val is not None:
            line_data = val.strip().split(",")
        else:
            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

Calling the generator and sending the value “Jane, 35”

guestlist = read_guestlist('guest_list.txt')
# for guest in guestlist:
for i in range(10):
    print(next(guestlist))
guestlist.send('Jane,35')
4 Likes
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") for i in range(10): print(next(guestlist)) guestlist.send('Jane, 35') for guest in guestlist: print(guest) over_21 = (key for key, val in guests.items() if int(val) > 21) for guest in over_21: print(guest) #Part 5 def chicken(): food = 'Chicken' table = 1 for i in range(5): seat = i + 1 yield f'Menu: {food}' yield 'table: {table}' yield f'seat: {seat}' def beef(): food = 'Beef' table = 2 for i in range(5): seat = i + 1 yield f'Menu: {food}' yield 'table: {table}' yield f'seat: {seat}' def fish(): food = 'Fish' table = 3 for i in range(5): seat = i + 1 yield f'Menu: {food}' yield 'table: {table}' yield f'seat: {seat}' #Part 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)

So I have seen an answer that works on another forum post, but I am wondering why my solution for Part 5 creates a weird result in Part 6. Why does putting yield on seperate lines for the menu, table and seat not give you all the information for each individual guest the same way that yield f’menu: {food}’, f’table: {table}’, f’seat: {seat}’ does.

For reference, the output of my code looks like this:

(‘Tim’, ‘Menu: Chicken’)
(‘Tonya’, ‘table: {table}’)
(‘Mary’, ‘seat: 1’)
(‘Ann’, ‘Menu: Chicken’)
(‘Beth’, ‘table: {table}’)
(‘Sam’, ‘Menu: Fish’)
(‘Manny’, ‘table: {table}’)
(‘Kenton’, ‘seat: 1’)
(‘Kenny’, ‘Menu: Fish’)
(‘Dixie’, ‘table: {table}’)
(‘Jane’, ‘Menu: Beef’)
(‘Mallory’, ‘table: {table}’)
(‘Julian’, ‘seat: 1’)
(‘Edward’, ‘Menu: Beef’)
(‘Rose’, ‘table: {table}’)

Each yield is separate so you’d need another iteration step to access each of those elements individually (another loop step/call to next for each item).

Your other example creates three new strings and yields them all at once in a single tuple object-

yield f'menu: {food}', f'table: {table}', f'seat: {seat}'

what these lines mean exactly?

“Val” is a value that we send to our generator. If there is no value, we get data from text_file, else the value is taken from yield

1 Like

oh I see, thank you buddy :slight_smile:

After Step 3 here, I tried it my way for a while, but eventually copied and pasted your code up through:

guestlist.send(‘Jane, 35’)
for guest in guestlist:
print(guest)

…When I print though, Jane is not in my output, only those in the original list…I cannot understand why that would be the case. Can anyone explain?

1 Like

I’ve not seen your full code but the following reply might be relevant- Intermediate Python 3 - Event Coordinator - #2 by tgrtim

Make sure you catch the output from the .send method. If you do send an input on the very last iteration you might find you need another use of next to get it back out again (the new value is sent to the generator but the generator would be paused at that point).

1 Like

He is right. This also does not work afais.

I am also confused how it that the right answer

not able to print the extra value added here

My solution for this project:

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


guest_generator = read_guestlist('guest_list.txt')


i=0
for guest in guest_generator:
  if i < 10:
    print(guest)
    i += 1
  else:
    break
#===========2==========================
print(guest_generator.send('Jane,35'))
#print(guests)
#============3=========================
print(next(guest_generator))
print(next(guest_generator))
print(next(guest_generator))
#print(next(guest_generator))

#==========4============================
drinks_generator = (name for name, age in guests.items() if age >= 21 )

# for i in drinks_generator:
#   print(i)

#=========5=============================
def Table_1():
  food = 'Chicken'
  table = 1
  for i in range(1,6):
    seat = i
    yield food, table, seat

def Table_2():
  food = 'Beef'
  table = 2
  for i in range(1,6):
    seat = i
    yield food, table, seat


def Table_3():
  food = 'Fish'
  table = 3
  for i in range(1,6):
    seat = i
    yield food, table, seat

fish_table = Table_3()
for i in range(5):
  print(next(fish_table))

def all_tables():
  yield from Table_1()
  yield from Table_2()
  yield from Table_3()

tab = all_tables()
# print(next(tab))
# print(next(tab))
#=============6=========================
#-----by generator function-----------
# def table_assigner(guests, all_tables):
#   for guest, table in zip(guests, all_tables):
#       yield guest, table

# assign = table_assigner(guests, tab)

#-------by generator expression--------
assign = ((guest, table) for guest, table in zip(guests, tab))
#-----result-------------------
for i in range(15):
  print(next(assign))
7 Likes

Given the all the feedback from this lesson, instructions need further clarity for this assignment. I understand this is an intermediate course. However, listen to your customers.

guests = {} # Task 1 def read_guestlist(file_name): text_file = open(file_name,'r') while True: 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 # yield line_data name = line_data[0] age = int(line_data[1]) guests[name] = age read_guestlist_iter = iter(read_guestlist('guest_list.txt')) for guest in range(1,10+1): print(next(read_guestlist_iter)) # Task 2 read_guestlist_iter.send('Jane,35') # Task 3 print(next(read_guestlist_iter)) print(next(read_guestlist_iter)) print(next(read_guestlist_iter)) # Task 4 guest_verify_age = (name for name in guests if guests[name] > 21) for guest in guest_verify_age: print(f"Verified over 21: {guest}") # Task 5 def table_one(): for seat in range(1,5+1): yield ("Chicken", "Table 1", f"Seat {seat}") def table_two(): for seat in range(1,5+1): yield ("Beef", "Table 2", f"Seat {seat}") def table_three(): for seat in range(1,5+1): yield ("Fish", "Table 3", f"Seat {seat}") def connected_table(): yield from table_one() yield from table_two() yield from table_three() connected_table_iter = iter(connected_table()) # Task 6 def table_assigner(guest_dict): for name in guest_dict: yield (name, next(connected_table_iter)) table_assigner_iter = iter(table_assigner(guests)) for guest in table_assigner_iter: print(guest)
1 Like

Why am I not printing anything? As others said, this makes no sense

1 Like

Googling for the answers is the greatest lesson for this project lol…way out of the scope for what i feel i was taught to understand through the course’s leading up to this. Regardless fun. Thank you community for helping…