[Tutor] Program xy.py

Alan Gauld alan.gauld at yahoo.co.uk
Fri Dec 22 14:16:02 EST 2023


On 22/12/2023 11:49, Ethan Rosenberg wrote:

> This program should put i*j  in a 2x2 matrix.  It does not.

Its always good to tell us what it does do including showing
the output. If the output is an error message (which I
assume is the case here!) its even more important to
paste the full error message into the mail.
It saves us guessing!


> mat=[][]
> for i =0:

That is not legal python, it should throw an error.
I'm not sure what you are even trying to do?

If it's just to set i to zero you don't need
the for, just use:

i = 0

And don't indent the next line

>     for j in range(0,1):
>         mat[0][j] = i*j

But if i is zero you don't need the math since
zero times anything is always zero.

mat[0] = [0,0]

> for j=0:
>     for i in range(0,1):
>         mat[i][0] = i*j

And the same here.
Except this only sets the first element so you
get the equivalent of

mat = [0,0][0]

I think you actually want something like

for i in (0,1):
   for j in (0,1):
      may[i][j] = i*j

Which should give you

mat = [0,0][0,1]

Is that what you were looking for?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos





More information about the Tutor mailing list