Print pattern with user given width and height

Viewed 285

Hello in python i would like to display in console something like thhis but with user given values. This should display when user give values 5 and 4. Its room with width 5 and height 4 units. It have to be with this stars and dots. I find some guides a try like this:

n=int(input("Enter the height:"))
for i in range(n+1):
    print("|     |")
    print("|     |")


    if i<n:
        print("......")

It display similar pattern but not as i expected.I would like also add this stars i the corner and option that user can specify width and height. It should look like room with walls. Could u help me with this. I've just started with python.


*-----*
|.......|
|.......|
|.......|
|.......|
*-----*
1 Answers

Here's a modification so you get what you want:

n=int(input("Enter the height:"))
print('*' + '-'*(n) + '*')
for i in range(n):
    print('|' + '.'*(n) + '|')
    
print('*' + '-'*(n) + '*')

Note that we are using a loop in range of (n) not in (n+1) so that it iterates n times exactly, and in that way borders are not counted from length.

If you want the user to add height and width, the code becomes like this:

n=int(input("Enter the height:"))
m=int(input("Enter the width:"))

print('*' + '-'*(m) + '*')
for i in range(n):
    print('|' + '.'*(m) + '|')
    
print('*' + '-'*(m) + '*')
Related