Doubt Exercise Python

Asked

Viewed 1,041 times

3

The challenge is: Write a program that receives as inputs (use the input function) two integers corresponding to the width and height of a rectangle, respectively. The program must print a string that represents the given rectangle with characters '#' in the output. My program, when I type 2 and 2 for height and width. inserir a descrição da imagem aqui

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

linha=0
coluna=0

while linha<a:
    while coluna<l:
        print("#", end=" ")
        coluna=coluna+1
    print()    
    linha=linha+1
    coluna=1
  • Place column = 0 at the end of the code. With column = 1 you start as if a '#'.

4 answers

5

The main problem is that you are rebooting the value of column 1 within the loop:

coluna=1

Switch to reset the column to 0 and your rectangle print will already be correct:

coluna = 0

The complete code would look like this:

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

linha = 0
coluna = 0

while linha < a:
    while coluna < l:
        print("#", end=" ")
        coluna = coluna + 1
    print()

    linha = linha+1
    coluna = 0

See online: https://repl.it/repls/OfficialAngryLibrary


One option is to use the loop for to solve this exercise in a very simple way:

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

for _ in range(a):
  print("# " * l)

See online: https://repl.it/repls/DarkgreyLimpingBoolean

4

Whoa, bro, the mistake that happens is that you are restarting the first While with the column equal to 1, and by it already start with 1 more, if you put 4, for example, it will only insert the 3 later "#".

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

linha=0
coluna=0

while linha<a:
    while coluna<l:
        print("#", end=" ")
        coluna=coluna+1
    print()    
    linha=linha+1
    coluna= 0 #Esse aqui foi o erro, você havia colocado "coluna= 1"

You need to restart the column with it getting 0, and this would already solve your problems.

2

The problem with your code is that you set the last line to 1 for the variable coluna. This way, your program will print a character "#" unless.

To fix the problem, just change the value 1 to zero.

coluna = 0

You can also improve your code in a few ways. Below are some tips:

  • Use the operator += to increment a value without having to repeat the variable.

    coluna = coluna + 1     
    coluna += 1         # É equivalente a linha de cima.
    
  • In your case, it is much better to use a loop for since it reduces some lines and makes the code more beautiful.

    for lin in range(altura):
        for col in range(largura):
            print("#", end = "")
        print()
    
  • No repeat loop required for width. Just use the operator * to repeat a string.

    for lin in range(altura):
        print("#" * largura)
    

See the code below and realize how simple your program can become:

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

for linha in range(altura):
    print("#" * largura)

2

Seeing the image, it was not clear whether or not to have a space between the #. In your code you put, but anyway, follows an alternative to both cases (I will not suggest the loops again because this has already been well addressed in the other answers, it is only to leave registered another alternative).


In Python it is possible to "multiply" a string by a number, so '#' * 2 produces the string '##'. Therefore, for each line it would be enough to do ('#' * largura) + '\n' (the # times the width to produce multiple #, and the \n for line breaking. Then, simply multiply this by the height to have multiple lines:

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

print((('#' * largura) + '\n') * altura)

I also changed the variable names to something more meaningful. It may seem like a silly detail, but giving better names helps a lot when programming, so make this habit right now.

If you want the spaces, just switch to '# ' * largura. Remembering that this will put an "extra" space at the end of the line. In addition, this solution (and some of the suggestions in the other responses as well) puts an extra blank line at the end. You can avoid this by creating lists and joining them with join:

# cria uma linha do retângulo, com os # separados por espaços (sem o espaço extra no final)
linha = ' '.join(['#'] * largura)
# cria várias linhas separadas por \n, sem a linha extra no final
print('\n'.join([linha] * altura))

Browser other questions tagged

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