Draw a rectangle using Python repeating structures

Asked

Viewed 6,054 times

6

I have to solve the following activity in Python:

I need to write a program that prints a rectangle with the full edges and the middle open:

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

But my show is doing this:

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

How to correct?

My code:

linha = int(input("digite a largura:"))
altura = int(input("digite a altura:"))

while  altura > 0:
   print("#", end = "")
   coluna = 2
   while coluna < linha: 
    if altura == 1 or coluna == linha:
        print("#",end="")
    else:
        print(end = " ")
    coluna = coluna + 1
   print("#")
   altura = altura - 1
  • Being Python, look for solutions pythonic. Tip: Do '#' * 10 returns to string ########## and make '#' + '_' * 8 + '#'returns #________# (replaces the white space with _ because of the formatting here of the comment). This would greatly decrease the complexity of your program.

  • I understand... but I need to automate for any width and height.

3 answers

6


Workaround

As commented, a simpler solution is to use the multiplication of strings in Python:

# Lê o tamanho do retângulo:
largura = int(input("digite a largura:"))
altura = int(input("digite a altura:"))

# Imprime a borda superior:
print('#' * largura)

# Imprime bordas laterais:
for _ in range(altura-2):
    print('#' + ' ' * (largura-2) + '#')

# Imprime borda inferior:
print('#' * largura)

Running the program:

>>> digite a largura: 10
>>> digite a altura: 5
##########
#        #
#        #
#        #
##########

See working on Repl.it.

Your solution

I analyzed your code and the main problem is that you change the value of altura to walk the lines. This makes your logic much more complex than it needs to be, as the program won’t know what the original rectangle height is. To get around this, control the line to be displayed with another variable.

# Aqui substitui de linha para largura:
largura = int(input("digite a largura:"))
altura = int(input("digite a altura:"))

# Defini a variável linha para controlar a linha a ser exibida
linha = 1

# Enquanto houver linha a ser exibida:
while  linha <= altura:

    print("#", end = "")
    coluna = 2

    # Substituído linha por largura também
    while coluna < largura: 

        # Se for a primeira linha, a última ou a última coluna
        if linha == 1 or linha == altura or coluna == largura:
            print("#",end="")
        else:
            print(end = " ")

        coluna = coluna + 1

    print("#")

    # Incrementa a variável linha ao invés de decrementar altura
    linha = linha + 1

Note that, thus, the variable altura remains unchanged and therefore its original reference is not lost. Running the program:

>>> digite a largura: 10
>>> digite a altura: 5
##########
#        #
#        #
#        #
##########

You have the expected exit.

See working on Repl.it.

  • I’m analyzing your code, seeing where the logic error is. I edit the answer by adding this as soon as possible.

  • Thank you very much... I had not understood the multiplication of strings Very interesting this option.

  • @Marcosvinicius edited the reply with comments on your code. See if I was clear enough.

  • It was totally clear... I understood the logical problem.

1

Be it a=height and w=width:

print(w*"#" + "\n"                                        #####
    + (a-2)*("#" + (w-2)*" " + "#\n")                     #   #   
    + w*"#")                                              #####

0

larg = int(input("Digite a largura: "))
alt = int(input("Digite a altura: "))

i=1
while i <=alt:
    if i==1 or i==alt:
        print(larg*"#")
        i+=1
    elif larg== alt:
         #print(larg*"#")
         print( *"#",(larg-3)*" ","#")
         i+=1


    else: 
        print( "#",(larg-(alt+1))*" ","#")
        i+=1
  • 1

    The line i += 1 can go outside the if, without repeating it; you have nothing to do 1*"#", nor define end = '\n' which is the default value of this parameter.

  • Why the negative? The code works! Please leave a comment

  • It also has the fact of not explaining what was done; and the fact of not generating the desired output (https://i.stack.Imgur.com/ls5bH.png);

  • @Can Woss help me fix it? I tried to help! worked with 4x4, 2x2 and 10x3 but really went to see here and failed with 10x10....

  • I submitted this solution to Oursera and they accepted...

Browser other questions tagged

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