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')
1 Like
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?

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).