473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1306
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
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...
0
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...
4
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...
46
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...
77
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....
9
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
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...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
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 <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.