[Tutor] can I make a while loop true again

Alan Gauld alan.gauld at btinternet.com
Tue Feb 11 12:55:30 CET 2014


On 11/02/14 11:06, Ian D wrote:

> Is it possible to restart a while loop?

Sorry, I'm sure you know what you mean but I'm not clear.
What do you mean by "restart a while loop"?

Do you mean after you exited it?
If so put the loop in a function and call the function again.

Do you mean from inside the loop?
If so use continue to jump back to the beginning.

Do you mean reset a counter of some sort so the
loop starts processing from the beginning?
Maybe you should be using a for loop?

I'm not clear what you want to do.

> This doesn't work at all (surprise surprise)

I'd expect it to work fine.
Whether what it does is what you wanted it to
do is a different question. What did you think
it might do? What are you trying to do?

I think you may want to add a test after the
while more loop to reset more. But I'm not sure?

while True:
    while more:
       # stuff here
    choice = input('do you want to go again?')
    if 'y' in choice.lower():
       more = True
    else:
       break

But I'm still not sure if that's really what you want?

> import turtle as t
>
> def start():
>      global more
>      more = True
>
> def stop():
>      global more
>      more = False
>
> more = True
>
> while True:
>      while more:
>          t.onkey(stop, "space")
>          t.onkey(start, "Up")
>          t.fd(1)
>          t.listen() 		 	   		

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list