Thread: Loops in Python
View Single Post
Old Jan 6th, 2007, 12:34 PM   #5
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Here is an example of using a recursive function as a while loop with a name:
def count_to_9(x):
    # do something with x
    print x
    x += 1
    if x >= 10:
        return
    count_to_9(x)

# start the loop at zero
count_to_9(0)

"""
output -->
0
1
2
3
4
5
6
7
8
9
"""
Note, this is just an example. Function calls are too time expensive to make this practical.

If you ever have to break out of some badly nested loops, put them into a function and use return to break out.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote