[Tutor] Newbie - Format question

Barbara Mark bl_mark@hotmail.com
Wed, 05 Dec 2001 22:39:02 -0600


Would someone help with question?  I’m trying to understand nested loops. I 
think I need a comma somewhere but I have tried all locations.  See my codes 
far below for the two most applicable.
I want to code the following output for any small n:

******************* *
1
2 4
3 6 9
.
.
n   2*n   …


******************* *
My pseudocode might be:
For any n,
start at 1
multiple by 1                          =   1        Loop 2
add 1 to that number, equals result a  =   2     This is loop 1

result a multipled by 1                =   2        Loop 2
result a multipled by 2                =   4        Loop 2
add 1 to result a, equals result b     =   3      This is loop 1

result b multipled by 1                =  3         Loop 2
result b multipled by 2                =  6         Loop 2
result b multipled by 3                =  9         Loop 2
add 1 to result b, equals result c     =  4      This is loop 1

In summary, I want to start with 1,
then add 1 each time around (1 + i = j),      =  2, then 3, then 4
then multiple 1 through j times for each:
1
2 4
3 6 9
********************* *
My coding gives gives the correct answer except there is a carrier return 
after each (see farther below).
But I want it to look like this:
1
2  4
3  6  9

I’ve tried commas in different locations, but I can’t get the desired 
result.  Would you help?  Thanks in advance.  - Barbara

Code is as follows:

>>>import sys
>>>i = 1
>>>while (i <= 3):
j = 1
while (j <= i):
  i * j
  j = j + 1
i = i + 1

1
2
4
3
6
9
****************** *
OR

>>>i = 1
>>>while (i <= 3):
j = 1
while (j <= i):
  print i * j,
  j = j + 1
i = i + 1


1 2 4 3 6 9
>>>

But I can’t get inbetween. i,e.
1
2 4
3 6 9




_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp