Python asks questions about array output

Asked 2 years ago, Updated 2 years ago, 154 views

I wrote a source code for 3x3 horsepower. But the output is It comes out in a way that includes [].

8 1 6

3 5 7

4 9 2

How can I print it out in this format?

def Printsquare(m):
    if m%2!=0:
        Magic =[[0 for i in range(m)] for j in range(m)]
        r=0;
        c=m/2;
        for i in range(1,m*m+1):
            Magic[r][c]= i
            br = r+1
            bc = c+1
            r=(r+m-1)%m
            c=(c+1)%m

            if Magic[r][c]!=0:
                r=br
                c=bc-1
    for row in Magic:
        print row

Printsquare(3)

python-2.7

2022-09-22 21:58

1 Answers

You can use "\t".join([str(num) for num in row]).

def Printsquare(m):
    if m%2!=0:
        Magic =[[0 for i in range(m)] for j in range(m)]
        r=0;
        c=m/2;
        for i in range(1,m*m+1):
            Magic[r][c]= i
            br = r+1
            bc = c+1
            r=(r+m-1)%m
            c=(c+1)%m

            if Magic[r][c]!=0:
                r=br
                c=bc-1
    for row in Magic:
      print("\t".join([str(num) for num in row]))
Printsquare(3)

For more information about join, please refer to the following link. http://tryhelloworld.co.kr/courses/ Python-introduction/lessons/list and strings


2022-09-22 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.