W1D2
Yesterday was a little challenging trying to keep up during probably my first ever pair programming. But it reminded me why I am here and that is to get better at programming. Finished writing the rest of the game (v barebone) in python and played around with writing the games w functions vs a class object and playing around with different implementations of generators.
Generator snippets below..
Method 1: Generator decides when to stop. The for loop just eats StopIteration.
for board in conway.play(2):
time.sleep(0.5)
Output: pre-yield - count 1 2026-02-18 10:30:00.123456 [loop body runs - sleep 0.5s] post-yield - count 1 2026-02-18 10:30:00.623456 i am simulating!!!! 2026-02-18 10:30:00.623789 pre-yield - count 2 2026-02-18 10:30:00.624012 [loop body runs - sleep 0.5s] post-yield - count 2 2026-02-18 10:30:01.124012 i am simulating!!!! 2026-02-18 10:30:01.124345 [generator exits, StopIteration caught by for-loop]
Method 2: Same as Method 1, but I explicitly call next() and catch the exception. Same thing the for-loop does under the hood.
conway_board_generator = conway.play(2)
while True:
try:
current_board = next(conway_board_generator)
time.sleep(0.5)
except StopIteration as e:
print("done iterating")
break
Method 3: Here I control how many times I call next(). So the iteration count lives in the loop, not in the generator.
conway_board_generator = conway.play(2)
for _ in range(2):
time.sleep(0.5)
current_board = next(conway_board_generator)
time.sleep(0.5)
Output: pre-yield - count 1 2026-02-18 10:30:00.123456 [sleep 0.5s] [sleep 0.5s] post-yield - count 1 2026-02-18 10:30:01.123456 i am simulating!!!! 2026-02-18 10:30:01.123789 pre-yield - count 2 2026-02-18 10:30:01.124012 [range(2) ends, loop exits]