Home > Software design >  For loop that outputs a boxed text
For loop that outputs a boxed text

Time:08-13

I'm just starting with python...again. Can you guys help me program a for loop that outputs this:

 ----- 
|World|
 ----- 

This is what I have so far...

name = '|World|'
for i in range(0,len(name)):
    print("-", end ='')
print ('\n', name)
for i in range(0,len(name)):
    print("-", end ='')

CodePudding user response:

def print_boxed(s):
    fix = f' {len(s)*"-"} '
    print(f'{fix}\n|{s}|\n{fix}')


print_boxed('World')
print_boxed('Blablabla')

prints

 ----- 
|World|
 ----- 
 --------- 
|Blablabla|
 --------- 

CodePudding user response:

On the first line, print a plus sign followed by a number of dashes equal to the length of the word followed by another plus sign.

On the second line, print a vertical bar followed by the word followed by another vertical bar.

On the third line, repeat the first line.

What is the difficulty?

  • Related