-1

See below where i am having trouble, i want to "return what the for return" let's say by this way, but i have no idea how to do it.

    def Matriz(m, n):

   return for i in range(0, m):
            for j in range(0, n):
               print(".", end=' ')
               print("\n")

I am not sure it this is possible, i would appreciate any type to how to make this work. The idea is to return, as you can see, a "lattice" of points like this

. . .

. . .

. . .

every time we ask for def, so that we don't need to type for in all place.

  • 1
    Does this answer your question? [Python: list of lists](https://stackoverflow.com/questions/11487049/python-list-of-lists) – Random Davis Nov 24 '20 at 19:26

2 Answers2

0

You do not need a return as you do not need to return a value if you are printing something.

def Matriz(m, n):

   for i in range(0, m):
        for j in range(0, n):
            print(".", end=' ')
            print("\n")
user14678216
  • 2,886
  • 2
  • 16
  • 37
0
def create_matrix(m, n):

    returnList = []

    # please note: you don't need to put both values if the first is 0
    # range(0, m) = range(m)
    for row in range(m): 
        returnList.append([])

        for column in range(n):
            returnList[row].append(".")

    return returnList

print(create_matrix(3, 3))
# [['.', '.', '.'], ['.', '.', '.'], ['.', '.', '.']]



def print_matrix(matrix):

    for row in matrix: 
        for column in row:
            print(column, end="")
        print("")

print_matrix( create_matrix(3, 3) )
# . . .
# . . .
# . . .

I've tried to answer your question as best I can. I'd suggest you first create a 2D list with rows and columns and then create a separate function to pretty-print it.

you can also use something called list comprehension to do this, which might be closer to what you are asking.

def create_matrix_comprehension(m, n):
    return [["." for column in range(n)] for row in range(m)]

print_matrix( create_matrix_comprehension(3, 3) )

doliphin
  • 752
  • 6
  • 22