Square Matrix in Python 3

1

My code is printing spaces at the beginning and end of the line, and it is giving presentation error (question 1557 URI) Does anyone have a good tip? "The values of the arrays must be formatted in a field of T size justified to the right and separated by space"

matriz = []

while True:

tamanho = int(input())

valor = 1

if tamanho == 0:

break

for i in range(tamanho):
linha = []
for j in range(tamanho):
  linha.append(valor)
  valor = valor*2
  if len(linha)==tamanho:

    valor = linha[0]

    valor = valor*2

matriz.append(linha)

tamanho_caracter=(str(matriz[-1][-1]))

tamanho_caracter=len(tamanho_caracter)

aux=tamanho_caracter+1

for a in matriz:

for b in a:

  convert1=str(b)

  print("{}".format(convert1).rjust(aux),end="".strip())

print()

print()  

matriz = [] 
    
asked by anonymous 11.11.2017 / 14:28

1 answer

3

The rjust method exists only to insert spaces at the beginning of a line. And you're calling the "strip", which removes spaces in an empty string, passed to the "end", which does nothing.

Read the python's print function documentation, see what "sep" and "end" are, try it in interactive mode: you'll understand better than if I write here. And keep in mind that in Python, indentation is not embellishment: the above code is not only syntactically invalid, it would not tell you what you wanted to do there, if you did not know what to print an array.

    
11.11.2017 / 15:43