473,721 Members | 3,997 Online

# Nested loops confusion

Hi,

I expect this is very obvious for anyone who knows what they're doing -
but I don't understand what's the problem with the following code. I
was intending that the program cycle through all i and j (ie. all
possible (i,j) coordinates, but the output when I run the program shows
me up to

"1
99
plot 3
1
100
plot 2
done j"

and doesn't perform the print functions for any i > 1.

Help much appreciated! :)

Matt
corna = int(raw_input(" CornA? "))
cornb = int(raw_input(" CornB? "))
side = int(raw_input(" Side? "))
i = 0
j = 0
for i in range(100):
for j in range(100):
x = corna + i * side / 100
y = cornb + j * side / 100
c = int(x^2 + y^2)
if c%3 == 1:
print i
print j
print "plot 1"
elif c%3 == 2:
print i
print j
print "plot 2"
elif c%3 == 0:
print i
print j
print "plot 3"
print "done j"
print "Done i"
May 11 '06 #1
6 1323
Oops, I forget to reset the j after the inner loop. Always manage to
work these things out just after asking for help! ;-)

Matthew Graham wrote:
Hi,

I expect this is very obvious for anyone who knows what they're doing -
but I don't understand what's the problem with the following code. I
was intending that the program cycle through all i and j (ie. all
possible (i,j) coordinates, but the output when I run the program shows
me up to

"1
99
plot 3
1
100
plot 2
done j"

and doesn't perform the print functions for any i > 1.

Help much appreciated! :)

Matt
corna = int(raw_input(" CornA? "))
cornb = int(raw_input(" CornB? "))
side = int(raw_input(" Side? "))
i = 0
j = 0
for i in range(100):
for j in range(100):
x = corna + i * side / 100
y = cornb + j * side / 100
c = int(x^2 + y^2)
if c%3 == 1:
print i
print j
print "plot 1"
elif c%3 == 2:
print i
print j
print "plot 2"
elif c%3 == 0:
print i
print j
print "plot 3"
print "done j"
print "Done i"

May 11 '06 #2
Thanks very much for the advice, have tidied it up and tested and seems
to be working as needed. I'm still not sure what was stopping the inner
loop from working earlier - but removing the redundancy in "j=0" and so
on seems to have solved it.

Matt

Dennis Lee Bieber wrote:
If that worked, you've got some weird code -- and it isn't what was
posted... Zeroing "j" does NOTHING, since the for loop assigns all
values to it, from 0..n-1

for loop indices do not need to be pre-initialized.

May 11 '06 #3
On 11/05/2006 5:59 PM, Matthew Graham wrote:
Thanks very much for the advice, have tidied it up and tested and seems
to be working as needed.
Seems to be working? Consider where you have the expression x^2 + y^2
.... I'd like to bet that you mean "x squared" etc, not "x exclusive-or
2" etc.
x = 3
x ^ 2 1 x ** 2 9

I'm still not sure what was stopping the inner
loop from working earlier
Call me crazy, but I thought you said it was the outer loop that wasn't
working ...

- but removing the redundancy in "j=0" and so
on seems to have solved it.

There's that "seems" word again. Removing "j=0" should have had no
effect at all, as Dennis has pointed out. What meaning do you attach to
"working"? Have you written any tests? Have you contemplated dropping
the size to say 5x5 instead of 100x100 and hand-calculating the expected
results?
May 11 '06 #4
>I'm still not sure what was stopping the inner
loop from working earlier - but removing the redundancy in "j=0" and so
on seems to have solved it.

Call me crazy, but be careful when programming python in different text
editors and in general, ie cutting and pasting, tabing and spacing.
Loops can look fine and not work (try moving around test print
statements for iterators), in this case try re-tabing your indents so
that everything is uniform in the editor you are using.

May 11 '06 #5
John Machin wrote:
Seems to be working? Consider where you have the expression x^2 + y^2
... I'd like to bet that you mean "x squared" etc, not "x exclusive-or
2" etc.
You're right, and I had already changed it after a look through a python
tutorial told me my mistake.
There's that "seems" word again. Removing "j=0" should have had no
effect at all, as Dennis has pointed out. What meaning do you attach to
"working"? Have you written any tests? Have you contemplated dropping
the size to say 5x5 instead of 100x100 and hand-calculating the expected
results?

True, I'm being sloppy, will test it properly when I have the time.
May 11 '06 #6
co************@ gmail.com wrote:
Call me crazy, but be careful when programming python in different text
editors and in general, ie cutting and pasting, tabing and spacing.
Loops can look fine and not work (try moving around test print
statements for iterators), in this case try re-tabing your indents so
that everything is uniform in the editor you are using.

the -tt flag is a good way to catch/avoid such problems:
#!/usr/bin/python -tt

May 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 25 12706 by: chad | last post by: I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will increase. Does Python have a limit on the number of nested for-loops? Thanks. 0 1788 by: Xah Lee | last post by: # -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return dict() print combo2(5) 4 7036 by: dw | last post by: Hello all. We're doing a site with teams and their members. We've got a page where we need to display people according to who belongs to a which team. I've heard that nested loops are bad, but what's the alternative? Would a group-by clause in the SELECT do the trick? Right now we're doing: (pseudo-code)--------------------------------- For each team print team name For each peson print person name 46 9925 by: Neptune | last post by: Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are already in a loop." Then he goes on to portray a contrived example that doesn't tell me under what conditions a nested loop might be favoured as a solution? i.e. what are nested loops useful for? What kinds of algorithms are served by nested loops?... 77 5220 by: Peter Olcott | last post by: http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop. Is this because .NET is inherently slower or does the C# compiler merely produce code that is not as well optimized as the C++ compiler? 9 2849 by: Gregory Petrosyan | last post by: I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs? 5 3123 by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by: Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and trying to understand Goto in this contect. PS: This is not homework. 13 2693 by: Fredrik Lundh | last post by: Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? 8 7257 by: Nathan Sokalski | last post by: I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If