


Then, at the end of the loop, set the variable equal to yield name. Ultimately, the solution would be to add a variable outside the while loop, then test to see if that variable was empty, and if not, format it and set it as line_data, otherwise to take line_data from the text file. This clearly wouldn’t be as simple as dropping in yield name at the end of the function.

It’s a comma-separated value such as those that the function has been parsing from guest_list.txt (curious why this isn’t guest_list.csv?). send(), as this later value is more than a name.
PYTHON TYPO GENERATOR GENERATOR
The next problem I ran into was the issue of modifying the function to become a generator yielding the name, yet also capable of accepting Jane,35 via. I added print("Continuing.") right before yield name to try and see if the loop was executing enough, and it was: Continuing.Īfter realizing that my earlier while loops had worked as expected, I realized that I was using next() to advance the iteration while for was iterating over it, resulting in…this weirdness. It wound up getting me wildly off track, though, because instead I tried to combine a for loop with a counter, like this: generator_object = read_guestlist("guest_list.txt")

I kept wondering, shouldn’t this be a while loop with a counter instead? Is this a typo? I now appreciate that the obvious solution was to use range() to iterate over, e.g. Next, I was bamboozled by the instruction to use a for loop to iterate through the first 10 guests. But I opted for the later, and appended yield name to the end of the while loop to make it a generator. I still don’t know whether this is asking me to yield the line read from the text file ( yield line_data) or yield the name extracted from that line ( yield name). Modify this function to be a generator function that will yield each read line so that each guest name is yielded each time for the generator. I’m less happy to report that I had trouble on nearly every step of this project.įirstly, in step 1, I was confused about this instruction: If you can work out at what point you can no longer track the flow then it might be easier to get useful guidance.Īfter digging through the forum, I’m happy to say a couple of different posts helped me get this sorted. If that’s no good then it might be worth clarifying exactly what’s tripping you up with generators. If you’ve come to the project and it’s not clear then it seems like a better choice to review the topic than to continue plowing ahead. send but generators themselves are exceedingly useful and much simpler than when you’re worrying about state whilst also sending values in. The lessons jump very quickly into the co-routine options and. Maybe spend a bit of time just playing around with basic generators and revisiting some of the earlier generator lessons if you’re completely lost. Those steps are things you’ll likely have covered before. In that project I believe you have to parse each line carefully to remove excess whitespace and to separate a guests name and age. For a basic method of yielding each line in turn from a text file something like the following might suit- def yield_file_lines(filepath):
