I am right now in the middle of the Event Coordinator project and I am stuck at the second step (For context, here is the link to the project. I am trying to use the send() method in order to add the guest to the guestlist, but for some reason I fail in doing so… Here is my code in case anyone can understand my problem and help me:
By and large you can treat next as equivalent to .send(None). But be aware that using .send will also return something from your generator (in addition to sending something in) so make sure you make use of it or you might miss some of your generator output, e.g. out = genobj.send(input).
I was having the same issue and if you read what the moderator is saying, he does provide the solution just not directly.
.send() is a function that is going to send whatever parameter you give it to your generator function and it’s going to expect a return because it’s going to perform the iteration just like next(). So what that means is that in order to see the it’s return you have to do something more than lst.send(“Jane,35”). It doesn’t print, because the generator doesn’t print BUT your iteration is coming back. So you could do one of two things:
Yes thank you for your reply. It took me a while reading all the posts then plugging away at it but I eventually figured it out. Once I got past that step I was pretty much ok from there.
I did it this way. It was very confusing. But I thing I got it
guests = {}
def read_guestlist(file_name):
text_file = open(file_name,'r')
n = None
while True:
if n != 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
guest_list = read_guestlist('guest_list.txt')
# Task 1:
# for index, guest in enumerate(guest_list):
# if index < 10:
# print(guest)
# else:
# print()
# break
for _ in range(20):
try:
if _ == 10:
print(guest_list.send('Jane,35'))
else:
print(next(guest_list))
except StopIteration:
print('_____')
break
over_21s = (name for name, age in guests.items() if age >= 21)
def table_1():
for seat_number in range(1,6):
yield ('Chicken', 'Table: C', f'Seat: {seat_number}')
def table_2():
for seat_number in range(6,11):
yield ('Beef', 'Table: B' , f'Seat: {seat_number}')
def table_3():
for seat_number in range(11,16):
yield ('Fish', 'Table: F', f'Seat: {seat_number}')
def all_tables():
yield from table_1()
yield from table_2()
yield from table_3()
tables = all_tables()
def table_assigner(guests):
for guest in guests:
yield(guest,next(tables))
seats = table_assigner(guests)
for seat in seats:
print(seat)