[Tutor] How do I create a "loop until" looping structure?

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Wed May 21 15:13:11 CEST 2014


On 21.05.2014 12:32, 1 2 wrote:
> Hi there,
>    As shown in the codes below, I want to end the loop until the s is no
> greater than -5 but I found there is no "do... loop until..." like C so
> what should I do? I will write it in C's style
>
> s,n = 0,1
> do:
>      import math
>      s=s+(math.log((n+1)/(n+2))/math.log(2))
> loop until s < -5
> print(s)
>

Hi,
there's no do .. until in Python, but while exists.
So just invert your condition and use it at the top of a while loop (as 
you could do in C as well).
Note also that your importing math inside the loop doesn't make sense. 
You want to import the module once in the beginning instead.
Best,
Wolfgang


More information about the Tutor mailing list