famguy74 famguy74 - 5 months ago 45
Python Question

Draw a box around message line

I asked this question earlier, but the answer I received ended up not working correctly so I started over completely. I now have more developed code but still can't figure out whats wrong and how to surround a and hello in a box like so:


The original question was: Given a message that may contain multiple lines, utilize the split() function to identify the individual lines, and use either of the formatting approaches we've learned as part of your solution to create the string that, when printed, draws a box around the message's lines, all centered. The box uses vertical bars and dashes on the sides (|, -), plusses in the corners (+), and there is always a column of spaces to the left and right of the widest line of the message. All lines are centered.

Here is the code I've come up with. I think it's on the right track but I'm getting errors especially with the ver function

def border_msg(msg):
for i in range(0,len(msg)):

count = count
for i in range (0,count+1):


print (ver)



To use count value in string formating you need

ver = "{}{:^{}}{}".format("|", msg, count,"|")

or using names - so it will be more readable for you

ver = "{a}{b:^{c}}{d}".format(a="|", b=msg, c=count, d="|")

but you can do also

ver = "|{a:^{b}}|".format(a=msg, b=count)

you can event add spaces in string

ver = "| {} |".format(msg)

Your code

def border_msg(msg):

    count = len(msg) + 2 # dash will need +2 too

    dash = "-"*count 


    print("| {} |".format(msg))


border_msg('a')     # without print
border_msg("hello") # without print

or without print inside function

def border_msg(msg):

    count = len(msg) + 2 # dash will need +2 too

    dash = "-"*count 

    return "+{dash}+\n| {msg} |\n+{dash}+".format(dash=dash,msg=msg)

print(border_msg('a'))     # with print
print(border_msg("hello")) # with print