473,556 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1312
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
12677
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
1783
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
7019
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...
46
9883
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...
77
5175
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
2833
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
3111
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
2683
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
7244
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 <Boolean ExpressionThen Exit For Next If Not <Boolean ExpressionThen Exit For
0
7624
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8060
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7907
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5171
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1161
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
865
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.