Challenge 3: A Fibonacci Generator — Possible Solution ==================================================================== def fibonacci(n): a, b = 0, 1 count = 0 while count < n: yield a a, b = b, a + b count += 1 for num in fibonacci(8): print(num) Output: 0 1 1 2 3 5 8 13 WHY THIS WORKS AS AN ANSWER ------------------------------ def fibonacci(n): containing a yield statement reuses this chapter's own count_up_to(n) structure exactly — the presence of yield anywhere in the function body is what makes calling fibonacci(8) return a generator object rather than immediately running the whole function. a, b = 0, 1 reuses tuple-unpacking assignment from Course 1 to track the two numbers needed to compute the next Fibonacci value. yield a pauses the function and hands back the current value, exactly like count_up_to's yield i — and just like that example, the function's local state (a, b, and count) is preserved automatically between calls, which is the entire reason a generator can produce a running sequence like this at all; a plain variable inside a regular function would reset every time the function was called again. a, b = b, a + b reuses simultaneous tuple assignment to advance both values in one step: b becomes the new a, and a + b becomes the new b — the standard Fibonacci recurrence, computed without needing a temporary variable. The for loop over fibonacci(8) reuses the chapter's own for num in count_up_to(5): pattern — each iteration resumes the generator right after its last yield, continuing until count reaches n (8), at which point the while loop's condition becomes false, the function reaches its natural end, and the generator raises StopIteration internally — which the for loop handles automatically, ending the loop cleanly.