Hello everyone, I am having a little bit of trouble with step 6 of this project. To help you get a better idea of what is going on, here is the part of my code that’s not working:
def seat_number_meal(guests, table1, table2, tale3):
names = list(guests.keys())
for i in range(5):
yield (names[i], next(table1))
for i in range(5):
i += 5
yield (names[i], next(table2))
for i in range(5):
i += 10
yield (names[i], next(table3))
meal_assignments = seat_number_meal(guests, table1(), table2(), table3())
for i in meal_assignments:
print(i)
As you can see there is a function (generator) with 3 for loop
that are supposed to iterate through the elements present in their respective bodies. In this sense, the first two loops work just fine, but the last one is outputting a TypeError: 'function' object is not an iterator
and I don’t understand exactly why that is. For further reference, here’s the entire code:
guests = {}
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
#if n is not None:
# n = n.split(',')
#name = n[0]
#age = int(n[1])
#guests[name] = age
# line_data.append(name)#, age
mini_list = read_guestlist("guest_list.txt")
for i in range(10):
print(next(mini_list))
mini_list.send('Jane,35')
for guest in mini_list:
print(guest)
guests_over_21 = (guest for guest in guests if guests[guest] >= 21)
for guest in guests_over_21:
print(guest)
def table1():
food = 'Chicken'
table = 1
for i in range(5):
seat = i + 1
yield f'menu: {food}', f'table: {table}', f'seat: {seat}'
def table2():
food = 'Beef'
table = 2
for i in range(5):
seat = i + 1
yield f'menu: {food}', f'table: {table}', f'seat: {seat}'
def table3():
food = 'Fish'
table = 3
for i in range(5):
seat = i + 1
yield f'menu: {food}', f'table: {table}', f'seat: {seat}'
def seat_number_meal(guests, table1, table2, tale3):
names = list(guests.keys())
for i in range(5):
yield (names[i], next(table1))
for i in range(5):
i += 5
yield (names[i], next(table2))
for i in range(5):
i += 10
yield (names[i], next(table3))
meal_assignments = seat_number_meal(guests, table1(), table2(), table3())
for i in meal_assignments:
print(i)
Thanks in advance for your help.