0

I want to add another guest to a generator, using send() method, that iterates over the guest_list.txt. I know I need to use the send() method, but I just can't understand the n = yield value logic.

The code below outputs the first 10 guests from the guest_list.txt. I want to add "Jane,35" to the guests that are being returned, as an 11th guest.

With;

guestlist_generator.send("Jane,35")

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
    yield name 



guestlist_generator = read_guestlist("guest_list.txt")
for i in range(10): 
    print(next(guestlist_generator))

guest_list.txt;

Tim,22
Tonya,45
Mary,12
Ann,32
Beth,20
Sam,5
Manny,76
Kenton,15
Kenny,27
Dixie,46
Mallory,32
Julian,4
Edward,71
Rose,65
  • The code shown is not runnable due to IndexError. May be that the indentation is wrong. It's unclear what you're trying to achieve. What are these "save" and "send" methods you refer to? – DarkKnight Apr 28 '22 at 07:57
  • My bad sorry, save() should have been send(). The send method's simple description according to Codecademy is; The .send() method allows us to send a value to a generator using the yield expression. Also, fixed the indentation I hope. – bloodymoonmate Apr 28 '22 at 08:12
  • This is a guided project that; wants me to add a guest with the information: "Jane,35". Using .send() method. Then wants me to print out the first 10 guest, which the code above does, plus Jane. @LancelotduLac – bloodymoonmate Apr 28 '22 at 08:21
  • Does this answer your question? [What's the purpose of "send" function on Python generators?](https://stackoverflow.com/questions/19302530/whats-the-purpose-of-send-function-on-python-generators) – tevemadar Apr 28 '22 at 08:29
  • @tevemadar thank you for the reply. I did check that topic, and tried to implement those on my code but failed. But I will look it up again, more focused. Thank you. – bloodymoonmate Apr 28 '22 at 08:44
  • @bloodymoonmate to be completely honest, I'm a bit puzzled too, because answers there seem to handle `next()` and `send()` separately, while based on both https://peps.python.org/pep-0342/#new-generator-method-send-value and https://docs.python.org/3/reference/expressions.html#generator.send my impression is that you use `send()` in place of `next()`. So practically instead of `print(next(guestlist_generator))` you would write `print(guestlist_generator.send())`. Which you could get in the `yield`, so `yield name` becomes `somevariable = yield name`. – tevemadar Apr 28 '22 at 08:54
  • And for a simple test you could just write `print(guestlist_generator.send(None))`, which should do exactly the same as `print(next(guestlist_generator))` – tevemadar Apr 28 '22 at 08:55
  • @tevemadar Thank you so much for the replies. In the course lessons before this project, Codecademy always used the `.send()` method within the for loop, so you are right it should be used with `next()` or in the for loop. Then they use, `somevariable = yield name` as you mention, also tried that but I fail to assign the sent value to the name, the sent value always returns `None`. I will check the links, and since English is not my first language it takes a bit time to really understand the syntax. Thank you again. – bloodymoonmate Apr 28 '22 at 09:16
  • I think I need a condition after the `somevariable = yield name` to assign the sent value to iterator. Can't figure that out. – bloodymoonmate Apr 28 '22 at 09:17

0 Answers0