python nub
python nub

Reputation: 11

How do i make this pattern from user input using a for loop (i want my code to be able to produce the output on the bottom)

Size = int(input("what is the size of the box "))

def Stars(Size):
  for x in range(0, Size):
    for y in range(0, Size):
      print("*",end="")
Stars()
OUTPUT (if the size of the box was 4)
        ****
        *==*
        *==*
        ****

Upvotes: 0

Views: 431

Answers (3)

python nub
python nub

Reputation: 11

def Stars(x = int(input("Enter size of box"))):  
   print("*" * x)  
   for i in range(1, x - 1):  
     print("*"+"="*(x - 2)+ "*")  
   print("*"* x)   
Stars()

Upvotes: 0

Anonymous
Anonymous

Reputation: 452

Try this lambda function

box = lambda columns: [print(('*'*columns if c==0 or c==columns-1 else '*'+'='*(columns-2)+'*')) for c in range(columns)]

Upvotes: 0

Mohammad
Mohammad

Reputation: 3396

for the first and last line, they should be outside the loop. Then you can create the loop normally,

def stars(x):
    print('*'*x)
    for i in range(1,x-1):
        print('*'+'='*(x-2)+'*')
    print('*'*x)

stars(4)

Upvotes: 1

Related Questions