![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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
"""# 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 |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
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
breakA 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 |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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
"""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! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |