Exercising nested loops in 3x python

1

I have been practicing with some simple Python 3x exercises that I got on the web, they are about nested for loops that draw geometric figures, however, there is one that I can not solve for more attempts I have made. The exercise goes like this:

  

Write a program that asks for the width and height of a rectangle and what   draw with asterisk characters "*" and hyphen "-":

Width: 6
Height: 4

The output should be something like this, a rectangle of asterisks filled with hyphens:

"* * * * * *
 * - - - - *
 * - - - - *
 * * * * * * "

However my code gives me this output:

"* * * * -
* * * * -
* * * * -
* * * * -
* * * * -
* * * * -
* * * * - "

Here I put my code in Python 3.6:

alto=int(input("Introduce la altura:"))
largo=int(input("ahora la longitud:"))
for a in range(alto):
    for l in range(1,largo):
        print("*",end=" ")
    print("-")

Thank you very much.

    
asked by orcaval 29.12.2017 в 08:04
source

3 answers

0

I give you a possible solution

alto=int(input("Introduce la altura:"))
largo=int(input("ahora la anchura:"))
for a in range(alto):
  if (a is not 0) and (a != alto - 1):
    for l in range(largo):
        if (l is not 0) and (l != largo - 1):
            print("-", end='')
        else:
            print("*",end="")
  else:
    for l in range(largo):
        print("*",end="")
  print('')
    
answered by 29.12.2017 / 10:08
source
1

I pass another possible answer using a variable named "drawing"

dibujo=''
anchura=20
altura=20


for i in range(0,altura):


    for j in range(0,anchura):
        if i ==0 or i == altura-1:
            dibujo+='*'
        elif j ==0 or j==anchura-1:
            dibujo+='*'
        else:
            dibujo +='-'
    dibujo+='\n'



print ("\n"*2)
print (dibujo)
    
answered by 02.10.2018 в 17:42
0

Replace dashes with spaces:

h=int(input("Introduce la altura del rectangulo: "))
a=int(input("Introduce la anchura del rectangulo: "))

for x in range(1):
    print("*"*a)

    for y in range(h-2):
        g=a-2#numero de guiones
        print("*"+(" "*g)+"*")

    print("*"*a)
    
answered by 09.01.2019 в 05:35