-2

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:

Example

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):
    count=len(msg)
    for i in range(0,len(msg)):
        count+=1

    count = count
    dash="-"
    for i in range (0,count+1):
        dash+="-"
    h="{}{}{}".format("+",dash,"+")

    count=count+2

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

print(border_msg('a'))
print(border_msg("hello"))
martineau
  • 119,623
  • 25
  • 170
  • 301
famguy74
  • 27
  • 3
  • 11

2 Answers2

2

Here's a possible solution for putting a box around something:

def box_lines(lines, width):
    topBottomRow = "+" + "-" * width + "+"
    middle = "\n".join("|" + x.ljust(width) + "|" for x in lines)
    return "{0}\n{1}\n{0}".format(topBottomRow, middle)

It expects lines which are properly split:

def split_line(line, width):
    return [line[i:i+width] for i in range(0, len(line), width)]

def split_msg(msg, width):
    lines = msg.split("\n")
    split_lines = [split_line(line, width) for line in lines]
    return [item for sublist in split_lines for item in sublist] # flatten

Putting box_lines and split_msg together, we get:

def border_msg(msg, width):
    return(box_lines(split_msg(msg, width), width))

We may use it as follows:

print(border_msg("""I'll always remember
The chill of November
The news of the fall
The sounds in the hall
The clock on the wall
Ticking away""", 20))

Which outputs

+--------------------+
|I'll always remember|
|The chill of Novembe|
|r                   |
|The news of the fall|
|The sounds in the ha|
|ll                  |
|The clock on the wal|
|l                   |
|Ticking away        |
+--------------------+
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • Your solution works really well but there is an issue with generating the "-" border. It says border_msg(hello): expected +-------+ but got +-----+. How would I have it generate two more "-" for the border? – famguy74 Oct 11 '16 at 02:12
  • @famguy74 There we go. This was harder than I thought. – Mateen Ulhaq Oct 11 '16 at 02:24
  • Thank you I've chosen someone else's answer as best answer because they completed it within one function though I may change it to yours as there are issues with formatting I will try using your answer as well – famguy74 Oct 11 '16 at 02:29
1

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(dash))

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

    print("+{}+".format(dash))


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
furas
  • 134,197
  • 12
  • 106
  • 148