Nested loops confusion

M

Matthew Graham

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"
 
M

Matthew Graham

Oops, I forget to reset the j after the inner loop. Always manage to
work these things out just after asking for help! ;-)
 
D

Dennis Lee Bieber

corna = int(raw_input("CornA? "))
cornb = int(raw_input("CornB? "))
side = int(raw_input("Side? "))
i = 0
j = 0

Unneeded, the FOR loop runs 0..99 and assigns those values to i and
j
for i in range(100):
for j in range(100):
x = corna + i * side / 100
y = cornb + j * side / 100

Define the equation better... Normal programming languages interpret
those as:

x = corna + ((i * side) / 100)

Also, note that if corna, cornb, and side are integers (as you
interpret them), the above will be integer arithmetic... This means that
the division will only result in a 0 until "i*side" is 100 or more... I
suggest you use 100.0 to get floating results (the multiply is safe).

So what do you really want?

x = corna + (( i * side) / 100.0)
x = corna + (i * (side / 100.0))
x = (corna + i) * (side / 100.0)
x = (corna + (i * side)) / 100.0
c = int(x^2 + y^2)

Try printing x and y (and c) to determine what is being produced...
if c%3 == 1:
print i
print j
print "plot 1"

Suggest putting those on one line:
print i, j, " plot 1"

Compare the results of (I'm using / 3, since I limited the loop to 3):
corna = 10
side = 3

for i in range(3):
print "test: ", i
print corna + ((i * side) / 3)
print corna + ((i * side) / 3.0)
print corna + (i * (side / 3))
print (corna + i) * (side / 3)
print (corna + (i * side)) / 3

test: 0
10
10.0
10
10
3
test: 1
11
11.0
11
11
4
test: 2
12
12.0
12
12
5
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Dennis Lee Bieber

Oops, I forget to reset the j after the inner loop. Always manage to
work these things out just after asking for help! ;-)

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.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
M

Matthew Graham

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
 
J

John Machin

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.
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?
 
C

conor.robinson

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.
 
E

Edward Elliott

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
 
M

Matthew Graham

John said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,299
Messages
2,571,545
Members
48,297
Latest member
MarcoStinn

Latest Threads

Top