Draw a square on the screen using a tick (#)

7

Hello, I'm starting to program in python and I'm having a hard time doing an exercise in which I have to make 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. Can anyone help me?

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

## 
##

The rectangle must be filled in. For example:
Width = 2 height = 2
should look like this: "##"
 "##"

    
asked by anonymous 19.01.2017 / 18:00

4 answers

11

Will this be?

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

STATEMENT

If you want to print the bottom rectangle (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 is, if we are in the first line, i == 0 , or in the last line, i == altura-1 , or in the first column, j == 0 , or in the last column j == largura-1 "we print" a "#" otherwise we print a empty space

DEMONSTRATION

    
19.01.2017 / 18:09
11

Another possible solution (with flexibilization of drawing with parameters for the border and fill):

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, '*', '%')

Result:

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

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

See running on Ideone .

    
19.01.2017 / 19:08
4
print ("#" * largura +"\n") * altura
    
20.01.2017 / 20:39
3

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="")
    
20.01.2017 / 06:20