There are three misunderstandings:
- The way the function was built the line:
print(retangulo(largura, altura))
is prolific because the entire printing process takes place within the function itself retangulo
then it can be called directly without the need of the print()
.
- The restriction in condition
coluna == largura - 1
is a contradiction for how you start counting columns from 1 your last column will have as an index the width itself.
- The condition
1 < linha < altura
is an option to correct error generated by the previous condition and therefore can be deleted.
By identifying the problems and modifying your code to work as you wish, you will be like this:
def retangulo(largura, altura):
linha = 1
while linha <= altura:
coluna = 1
while coluna <= largura:
if linha == 1 or linha == altura or coluna == 1 or coluna == largura:
print("#", end="")
else:
print(" ", end="")
coluna += 1
print()
linha += 1
retangulo(largura, altura)
Test the code on Repl.it
As, says the Maniero, working is not the same as being right, your code can be rewritten and simplified with a crease, f-string and the operator of sequence repeat *:
largura = int(input("Digite a largura: "))
altura = int(input("Digite a altura: "))
def retangulo(largura, altura):
#Itera em l de 0 até altura - 1...
for l in range(altura):
if l == 0 or l == altura - 1:
print("#"*largura) #...se a linha for ou topo ou a base imprime quantos # quanto largura.
else:
print(f"#{' '*(largura-2)}#") #...para todas as outras linhas imprime a primeira e última colunas.
retangulo(largura, altura)
Test the code on Repl.it
Or else using the ternary operator:
largura = int(input("Digite a largura: "))
altura = int(input("Digite a altura: "))
def retangulo(largura, altura):
for l in range(altura):
print("#"*largura if l == 0 or l == altura - 1 else f"#{' '*(largura-2)}#")
retangulo(largura, altura)
Test the code on Repl.it