Help with the generator project

Hey everyone,
I’ve been working on the Intermediate Python 3 course and have gotten stuck on the generator project Event Coordinator and was hoping I could get some help from the group.

The second part of the project requires you to use the send method to input a new guest. The code I have is as follows:

guests = {}
def read_guestlist(file_name):
text_file = open(file_name,‘r’)
while True:
line_data = text_file.readline().strip().split(",")
new_guest = yield line_data
if new_guest != None:
line_data = new_guest
if len(line_data) < 2:
name = line_data[0]
age = int(line_data[1])
guests[name] = age

gen_obj = read_guestlist(‘guest_list.txt’)
for name in range(10):
print(gen_obj.send(‘Jane, 35’))

The output of the code is as follows:
[‘Tim’, ‘22’]
[‘Tonya’, ‘45’]
[‘Mary’, ‘12’]
[‘Ann’, ‘32’]
[‘Beth’, ‘20’]
[‘Sam’, ‘5’]
[‘Manny’, ‘76’]
[‘Kenton’, ‘15’]
[‘Kenny’, ‘27’]
[‘Dixie’, ‘46’]
Traceback (most recent call last):
File “”, line 24, in
print(gen_obj.send(‘Jane, 35’))
File “”, line 14, in read_guestlist
age = int(line_data[1])
ValueError: invalid literal for int() with base 10: ‘a’

The send method should produce the outcome of adding [‘Jane’, ’35’] to the list above. I’m unsure of what I’m doing wrong and any help would be greatly appreciated.

The specific exercise is

1 Like

Please see How do I format code in my posts? for posting code to the forums (or consider some form of online code hosting) as it can be very difficult to interpret Python code without indentation.

You should have a look at the contents of your line_data name when you use .send to provide an additional guest (perhaps print or similar could help). It seems very likely to be in a different format to what you expect.

As an aside it might be worth reviewing any lessons on .send. You should be careful about advancing iteration when .send is used as you have the potential to skip/miss elements from the original generator if you’re not careful, some control flow to avoid such an issue may be useful. Double-check your input/output to make sure your generator actually yields everything that you want it to.