Stopping the Repetition

The common term infinite loop refers to a sequence of code that will repeat forever. A simple example just sets up a while ... : statement that tests against something that is always going to result in True. For instance, just using True will always work. You should not type in the following code, because it's the kind of thing that's better to see than to have to do yourself:

... print "You're going to get bored with this quickly"

... print "You're going to get bored with this quickly"

You1

re

going

to

get

bored

with

this

quickly

You1

re

going

to

get

bored

with

this

quickly

You1

re

going

to

get

bored

with

this

quickly

You1

re

going

to

get

bored

with

this

quickly

You1

re

going

to

get

bored

with

this

quickly

The preceding code continues forever or until you break out of it. Inconvenient as it seems at first glance to have something that repeats forever, there are times you may want this—for instance, in a program that waits for the user to type something in, and when the user is done, returns to waiting.

However, sometimes you will want to know that if certain conditions are met, such as the right time of day, when the water has run out, when there are no more eggs to be made into omelets, and so on, that the repetition can be broken out of even when there is no explicit test in the top of the while ... : or when the list that's being used in the for ... in ... : doesn't have an end.

Infinite loops can be exited by using the break statement. Some of the lines in the example here continue for a long time. When you try this out, if you see a line that doesn't begin with a >>> or a . . ., then it's actually part of the prior line, so type the entire line. In addition, make sure your indentation matches what's on the page:

>>> omlettes_ordered = 5 >>>

omlettes_delivered = 0

>>> fridge_contents = {"egg":

8, "mushroom":20, "pepper":3, "cheese":2, "tomato":4,

"milk":13}

>>>

>>> while omelets_delivered <

omelets_ordered:

... break_out = False

... for ingredient in omelet_ingredients.keys():

... ingredients_needed = omelet_ingredients[ingredient]

... print "adding %d

%s to the mix" % (omelet_ingredients[ingredient],

ingredient)

... fridge_contents[ingredient] = fridge_contents[ingredient] -

ingredients_needed

... if fridge_contents[ingredient] < ingredients_needed:

... print "There

isn't enough %s for another omelet!" % ingredient

... break_out = True

... omelets_delivered = omelets_delivered + 1

... print "One more omelet made! %d more to go" % (omelets_ordered -

omelets_delivered)

... if break_out == True:

... print "Out of ingredients, go shopping if you want to make more

omelets!"

... break

adding 1 cheese to the mix

adding 1 pepper to the mix

adding 2 egg to the mix

adding 1 milk to the mix

adding 5 mushroom to the mix

One more omelet made! 4 more

to go

adding 1 cheese to the mix

There isn't enough cheese for

another omelet!

adding 1 pepper to the mix

adding 2 egg to the mix

adding 1 milk to the mix

adding 5 mushroom to the mix

One more omelet made! 3 more

to go

Out of ingredients, go shopping if you want to make more omelets!

If you use break, it will only take you out of the most recent loop — if you have a while ... : loop that contains a for ... in ... : loop indented within it, a break within the for ... in ... : will not break out of the while ... :.

Both while ... : and for ... in ... : loops can have an else: statement at the end of the loop, but it will be run only if the loop doesn't end due to a break statement. In this case, else: could be better named something like done or on_completion, but else: is a convenient name because you've already seen it, and it's not hard to remember.

Was this article helpful?

0 0

Post a comment