exiting a while loop

D'Arcy Cain darcy at VybeNetworks.com
Fri May 22 08:02:22 EDT 2020


On 2020-05-22 7:49 a.m., John Yeadon via Python-list wrote:
> Am I unreasonable in expecting this code to exit when required?

Yes.

> # 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.")

I think that you meant "break", not "exit".

-- 
D'Arcy J.M. Cain
Vybe Networks Inc.
A unit of Excelsior Solutions Corporation - Propelling Business Forward
http://www.VybeNetworks.com/
IM:darcy at Vex.Net VoIP: sip:darcy at VybeNetworks.com


More information about the Python-list mailing list