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 )

print('Hello world!')
1 Like

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