exiting a while loop

Souvik Dutta souvik.viksou at gmail.com
Fri May 22 07:56:57 EDT 2020


No not really. If you use a breakpoint and a debugger you will find out
that your code continues till 2 M is met and then stops printing numbers
continuously.

On Fri, 22 May, 2020, 5:20 pm 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.")
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list