Learn Intermediate Python 3 - Python Event Coordinator Project

Hi everyone,

I am having an issue with the following question:

We’ve printed out our first 10 guests when our phone rings! It’s our customer who has another guest to add to the guestlist. She wants to add a guest with the information: “Jane,35”. Use one of the three generator methods we have learned to accomplish this.

I am trying to add “Jane,35” using the send() method but it returns StopIteration.

Here is my code:

guests = {}

def read_guestlist():

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

    yield name

guestlist = read_guestlist()

for k in range(1,10):

print(next(guestlist))

print(guestlist.send(“Jane,35”))

#print(next(guestlist))

for k in range(11,16):

print(next(guestlist))

Could anyone provide me with the correct code for this problem? Thank you

Maybe you should review how the .send method works and how to organise a generator in that case, was it covered in a previous lesson?

There are only 14 entries in txt file and you running your range from 11 to 16 which cause the error. Make your range that way that you can get 3 entries or else use print(next(guestlist)) 3 times. Can you tell me why your are yielding the name instead of line_data?

Hi,

I am yielding the name since the question requires me to print the first 10 guests, so I am assuming I should print the name of the guests instead of the list of guests. Nevertheless, I am still confused on how send method works. I implemented .send() method to insert the value of “Jane,35” after the first 10 entries were printed, but it keeps returning stop iteration. Could you please explain to me what’s happening and how to solve this? Thank you

You are yielding name only and passing the value as “Jane,35” which should be only “Jane”. If you see the instruction you can see that they saying to yield line_data so that we can send “Jane,35” as value. I have tried to implement that way but I am getting another error of NoneObject passed. If I use if statement to resolve that error, its not giving me the all entries into guests dictionary. So I am also confused what to do.

Hi,

Thank you for your suggestion. I have amended my code to the following:

guests = {}

def read_guestlist():

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

    yield line_data                        

guestlist = read_guestlist()

for k in range(1,11):

print(next(guestlist))

guestlist.send(“Jane,35”)

print(next(guestlist))

for k in range(11,15):

print(next(guestlist))

After running the code, I run into the following error:
Traceback (most recent call last):
File “script.py”, line 24, in
print(next(guestlist))
StopIteration.

Here, line 24 refers to the for k in range(11,15): print(next(guestlist)) error. I am trying to amend the read_guestlist() code to accept “Jane,35” as an input, but the original code itself is modifying the .txt file instead of the string. Do you know how to get around this problem?

Thank you

This was the code I used to clear this project. It is not the cleanest, but… it get the job done

guests = {}
def read_guestlist(file_name):
    text_file = open(file_name,'r')
    while True:
        x = yield
        if x == None:
          line_data = text_file.readline().strip().split(",")
        else:
          line_data = x.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
        yield name
        
guest_list = read_guestlist("guest_list.txt")

#task 1
for guest in range(10):
  next(guest_list)
  print(next(guest_list))

#task 2
next(guest_list)
guest_list.send("Jane, 35")

#task 3
for guest in range(8):
  next(guest_list)

#task 4
over_21 = (name for name in guests if guests[name] >= 21)

#task 5
def table1_generator():
  for i in range(1, 6):
    yield "Chicken", "Table 1", "Seat {}".format(i)
def table2_generator():
  for i in range(1, 6):
    yield "Beef", "Table 2", "Seat {}".format(i)
def table3_generator():
  for i in range(1, 6):
    yield "Fish", "Table 3", "Seat {}".format(i)
table1 = table1_generator()
table2 = table2_generator()
table3 = table3_generator()

#task 6
def assign_table_generator(guests):
  for guest in guests:
    guest_code = list(guests.keys()).index(guest) + 1
    if guest_code <= 5:
      yield (guest, next(table1))
    elif guest_code <= 10:
      yield (guest, next(table2))
    else:
      yield (guest, next(table3))
table = assign_table_generator(guests)

Hi Raul,

Thank you for your code. I have fixed it and here is my solution:

guests = {}
def read_guestlist(file_name):
    text_file = open(file_name,'r')
    while True:
        x = yield
        if x is None:
          line_data = text_file.readline().strip().split(",")
        else:
          line_data = x.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
        yield name 
                                            
guestlist = read_guestlist('guest_list.txt')

for k in range(20):
  print(next(guestlist))

#Add Jane,35 to the generator
next(guestlist)
guestlist.send("Jane,35")

for k in range(8):
  print(next(guestlist))

def guest_age(guest_dict):
  for key,val in guest_dict.items():
    if val > 21:
      yield key

age_of_guest = guest_age(guests)

for obj in age_of_guest:
  print(obj)

def table_one():
  for j in range(1,2):
    for k in range(1,6):
      yield "Chicken", "Table {}".format(j),"Seat {}".format(k)

def table_two():
  for j in range(2,3):
    for k in range(6,11):
      yield "Beef", "Table {}".format(j), "Seat {}".format(k)

def table_three():
  for j in range(3,4):
    for k in range(11,16):
      yield "Fish", "Table {}".format(j), "Seat {}".format(k)


def combined_tables():
  yield from table_one()
  yield from table_two()
  yield from table_three()

combined_tables = combined_tables()

def guest_assignment():
  for name in guests.keys():
    yield name, next(combined_tables)

guest_assignment = guest_assignment()
for person in guest_assignment:
  print(person)
1 Like