Draw square on the canvas using wire fences (#)

Asked

Viewed 5,611 times

10

Hello I’m starting to program in python and I’m having a hard time doing an exercise where I have to do a rectangle with "#".

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

while largura > 0:
    print("#", end = "")
    while altura > 0 :
         altura = altura - 1

    largura = largura - 1 

However, I can make the width but not the height. Someone can help me?

The rectangle must be filled from the inside. For example: Width = 2 height = 2 should go like this:

## 
##

The rectangle must be filled inside. For example:
Width =2 height = 2
should come out like this:
"##"
"##"

  • Angelo for the rectangle to be filled in my first example or @Luizviera already do that

5 answers

13

Is this?

largura = int(input("Digite a largura: "))
altura = int(input("Digite a altura: "))
for _ in range(altura): # por cada linha
    print('#'*largura) # imprimimos a largura * #

DEMONSTRATION

If you want to print the rectangle without background (only the edges) in python2 or python3:

largura = int(input("Digite a largura: "))
altura = int(input("Digite a altura: "))
rect = ''
for i in range(altura):
    for j in range(largura):
        if(j == 0 or i == 0 or j == largura-1 or i == altura-1):
            rect += '#'
            continue
        rect += ' '
    rect += '\n'
print(rect)

That’s if we’re on the front line, i == 0, or on the last line, i == altura-1, or in the first column, j == 0, or in the last column j == largura-1 "print" a "#" or print an empty space

DEMONSTRATION

  • It has how to do using only while?

  • @Angelospinardi dá sim, http://ideone.com/UJLqIJ is there with while

  • Thank you Miguel!!

13

Another possible solution (with flexibility of the drawing with parameters for the border and filling):

def desenhaQuadrado(altura, largura, simbolo = '#', preenchimento = ' '):
    print(simbolo * largura)
    for _ in range(altura-2):
        print('{}{}{}'.format(simbolo, preenchimento * (largura - 2), simbolo))
    print(simbolo * largura)

print('Um quadrado:')
desenhaQuadrado(7, 10)

print('\nOutro quadrado:')
desenhaQuadrado(4, 8, '*', '%')

Upshot:

Um quadrado:
##########
#        #
#        #
#        #
#        #
#        #
##########

Outro quadrado:
********
*%%%%%%*
*%%%%%%*
********

See spinning in Ideone.

  • 1

    Good, the use of function is good idea

  • @Miguel I only posted an alternative because the AP said it’s for language study. :)

  • 1

    Of course, I also liked the logic

  • And yours has more performance because executed only in one cycle

  • @Miguel This is not necessarily true, as its internal tie is implicit in my use of the operator * applied to the filler. : ) But not that it makes much difference in performance even.

  • 1

    Luiz; very cool! + 1

Show 1 more comment

5

print ("#" * largura +"\n") * altura
  • Very good Jjoao

  • @Miguel, thank you. It’s called the lazy version :) . By the way, I learned a few things from your answer.

  • 1

    I think that’s the biggest reason we’re here, that and helping each other

5

A simple approach:

width = int(input("Digite a largura: "))
height = int(input("Digite a altura: "))

for _ in range(0, height):
    for _ in range(0, width):
        print("#", end="")
    print("\n", end="")

1

I did so:

def retangulo (larg,alt):
    carac = "#"

    for _ in range(alt):
        print(larg*carac)

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

retangulo(larg,alt)

Browser other questions tagged

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