Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
What does this program show? Say it before you run it.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
Plan. Nothing in gen runs until the first next. Each next runs to the next yield.
g = gen(): nothing runs.print("start"): shows start.print(next(g)): runs print("a"), and gives 1. Shows a, then 1.print(next(g)): runs print("b"), and gives 2. Shows b, then 2.for x in g:: continues after the second yield: runs print("c"), and the body ends. No more values, so the loop body never runs. Shows c.print("end").Answer: start, a, 1, b, 2, c, end, each on its own line. The word never is not shown.
Log in to join the discussion.
Loading…