0
def drawme_five(n):

    a = [['.']*n]*n
    for i in range(len(a)):
        for j in range(len(a[i])):
            if i == 0 or i == len(a)-1 or i == int(len(a)/2):
                a[i][j] = '*'
            if i < int(len(a)/2):
                a[i][0] = '*'
            elif i > int(len(a)/2):
                a[i][len(a)-1]='*'

    return a

I expected this code to give an output of a list of lists, where it with '.' and '*' in the form of number 5, but it gives me an output of all asterics. I don't know why the if statements don't work. If it works correctly, and we print one list each line, the output would be in a form of 5, for example if n = 5


  • . . . .

. . . . *


Shahane
  • 3
  • 3

1 Answers1

0

If you create a 2D matrix in Python by initialising it as [['something']*n]*m, then all lists inside the main list will point to the same location. If edit even one sublist, all sublists will get edited.
Try initialising it as
lst = [['something' for i in range(m)] for j in range(n)]
to get a 2D Matrix of n x m

Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24