title: Understanding the use of "while True" in a control flow Python generator function
body: <p>I am trying to understand the use of the <code>while True</code> statement  in the following Python code. It uses a generator and appears in the 'Visualizing the flow' section of this <a href="https://jeffknupp.com/blog/2013/04/07/improve-your-python-yield-and-generators-explained/" rel="nofollow noreferrer">webpage</a>. </p>
  
  <p>The code is an example that attempts to solve <a href="https://projecteuler.net/problem=10" rel="nofollow noreferrer">Project Euler problem 10</a>. Here, we use <code>get_primes</code> with a start value of 3. The <code>solve_number_10</code> function calls <code>get_primes</code> until it hit the last prime number up to 2,000,000.</p>
  
  <pre><code>def solve_number_10():
  # She *is* working on Project Euler #10, I knew it!
      total = 2
      for next_prime in get_primes(3):
          if next_prime &lt; 2000000:
              total += next_prime
          else:
              print(total)
              return
  
  def get_primes(number):
      while True:
          if is_prime(number):
              yield number
          number += 1
  </code></pre>
  
  <p>The author writes:</p>
  
  <blockquote>
    <p>If a generator function calls return or reaches the end its
    definition, a <code>StopIteration</code> exception is raised. This signals to
    whoever was calling <code>next()</code> that the generator is exhausted (this is
    normal iterator behavior). <strong>It is also the reason the <code>while True:</code> loop
    is present in <code>get_primes</code>. If it weren't, the first time <code>next()</code> was
    called we would check if the number is prime and possibly yield it. If
    <code>next()</code> were called again, we would uselessly add 1 to number and hit
    the end of the generator function (causing <code>StopIteration</code> to be
    raised).</strong> Once a generator has been exhausted, calling <code>next()</code> on it
    will result in an error, so you can only consume all the values of a
    generator once. The following will not work:</p>
  </blockquote>
  
  <pre><code>our_generator = simple_generator_function()
      for value in our_generator:
          print(value)
  
  print(next(our_generator))
  </code></pre>
  
  <p>Why do we hit the end of the generator function in the absence of the <code>while true</code> statement? What exactly do we need to declare as <code>true</code>? Is it that the <code>number</code>is less than 2,000,000? I do not think this is the case, since <code>solve_number_10</code> takes care of that. I don't understand why the generator function would end in the second iteration, given the absence of the <code>while True</code> statement, if there is a long way until <code>number</code> hits 2,000,000.</p>
  
  <p>I would really appreciate it if someone could explain the bolded text in the yellow quote block. Thank you!</p>