Problem where I have to create a rectangle without padding, but I’m having trouble with the additional spaces in the rectangle

Asked

Viewed 41 times

-1

Hello, I have to make a rectangle without fill, example:

digite a largura: 10
digite a altura: 3
##########
#        #
##########`

But, because of the course automatic corrector, I am not able to proceed on account of two spaces on the right side in the middle of the rectangle, example:

##########
#        #[ESPAÇO][ESPAÇO]
##########

I don’t know how to solve this problem, this is the code:

largura = int(input("Digite a largura: "))
altura = int(input("Digite a altura: "))
    
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 - 1:
                print("#", end="")
            if 1 < linha < altura:
                print(" ", end="")
            coluna += 1
        print()
        linha += 1
        

print(retangulo(largura, altura)) 

1 answer

1

There are three misunderstandings:

  1. 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() .
  2. 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.
  3. 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.