exiting a while loop

Larry Martell larry.martell at gmail.com
Fri May 22 07:55:00 EDT 2020


On Fri, May 22, 2020 at 7:51 AM John Yeadon via Python-list
<python-list at python.org> wrote:
>
> Am I unreasonable in expecting this code to exit when required?
>
>
> # Add up the powers of 2 starting with 2**0 until 2 million is met.
> n = 1
> target = 2000000
> sum = 0
>
> while True:
>      x = 2 ** (n - 1)
>      sum += x
>      print(n, sum)
>      if sum >= target:
>          print("Target met.")
>          exit
>      n += 1
>
> print("\n", n, "terms are required.")

exit()


More information about the Python-list mailing list