Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 5th, 2007, 12:45 PM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Loops in Python

This question came from a hijacked thread. I thought it important enough to put it in its own thread with the proper title.

What loops does Python offer and what are they used for?

I might add, that our friend Arevos gave one good answer here:
http://www.programmingforums.org/for...122207-30.html
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jan 5th, 2007, 12:49 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I don't think you can get a much better answer than his reply. Certainly his first sentence is virtually THE definitive answer to the second question. I agree with your reason for restating it here, however.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 5th, 2007, 1:56 PM   #3
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
There are several ways to stop a while loop:
# the while loop needs a True condition, here x < 10 to run
# and will stop running on False, when x goes to 10 or above
x = 0
while x < 10:
    # do something with x
    print x
    # increment x by 1, same as x = x + 1
    x += 1

print

# 'while True' would be an endless loop, so you need to break it
# with a break condition (an if statement), stops when x is 10 or higher
x = 0
while True:
    # do something with x
    print x
    x += 1
    if x >= 10:
        break

"""
output in either case -->
0
1
2
3
4
5
6
7
8
9
"""
With the while loop you can do cute things like this:
# whittle down a string with while:
s = "internets"
# string s is True until it's empty
while s: 
    print s
    # each time around remove one char from the end
    s = s[:-1]

"""
output -->
internets
internet
interne
intern
inter
inte
int
in
i
"""
__________________
I looked it up on the Intergnats!

Last edited by Dietrich; Jan 5th, 2007 at 2:24 PM. Reason: more
Dietrich is offline   Reply With Quote
Old Jan 6th, 2007, 5:09 AM   #4
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Another way of breaking a while loop - or even nested while loops -- is to use a flag. If you find yourself using this technique, though, you probably need to redesign:
flag1 = False
flag2 = False
while not (flag1 or flag2):
    for i in range(10):
        print i
        if i == 4:
            flag1 = True
            break
        elif i == 6:
            flag2 = True
            break

A sometimes useful trick for getting out of nested loops is to raise an exception - StopIteration is the clearest, (use sparingly - a redesign/restructure is normally better):
def myfunc(i, j):
    print i, j
    if i == j == 1:
        raise StopIteration
    
try:
    while True:
        for i in range(10):
            for j in range(10):
                myfunc(i, j)
except StopIteration:
    pass
-T.
hydroxide is offline   Reply With Quote
Old Jan 6th, 2007, 11:34 AM   #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
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Perfect Python Dietrich Python 30 Jan 5th, 2007 12:38 PM
[tutorial] Python for programming beginners coldDeath Python 30 Dec 14th, 2005 11:35 AM
Convert Python script to C++ code clanotheduck Python 17 Sep 25th, 2005 8:55 AM
Advanced Python Tricks Arevos Python 19 Sep 24th, 2005 7:39 AM
Python - A Programmers Introduction coldDeath Python 17 Aug 19th, 2005 12:41 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC