Seeing the image, it was not clear whether or not to have a space between the #
. In your code you put, but anyway, follows an alternative to both cases (I will not suggest the loops again because this has already been well addressed in the other answers, it is only to leave registered another alternative).
In Python it is possible to "multiply" a string by a number, so '#' * 2
produces the string '##'
. Therefore, for each line it would be enough to do ('#' * largura) + '\n'
(the #
times the width to produce multiple #
, and the \n
for line breaking. Then, simply multiply this by the height to have multiple lines:
largura = int(input("Digite a largura: "))
altura = int(input("Digite a altura: "))
print((('#' * largura) + '\n') * altura)
I also changed the variable names to something more meaningful. It may seem like a silly detail, but giving better names helps a lot when programming, so make this habit right now.
If you want the spaces, just switch to '# ' * largura
. Remembering that this will put an "extra" space at the end of the line. In addition, this solution (and some of the suggestions in the other responses as well) puts an extra blank line at the end. You can avoid this by creating lists and joining them with join
:
# cria uma linha do retângulo, com os # separados por espaços (sem o espaço extra no final)
linha = ' '.join(['#'] * largura)
# cria várias linhas separadas por \n, sem a linha extra no final
print('\n'.join([linha] * altura))
Place column = 0 at the end of the code. With column = 1 you start as if a '#'.
– Leonardo Araújo