-3

I'm working on this Python task that I can't figure out. It is the last of 3 functions and the first 2 were much easier to program then this one. The instructions are "Given a message that may contain multiple lines, utilize the split() function to identify the individual lines, and the format() function so that when printed, it draws a box around the message's lines, all centered. Box uses vertical bars & dashes on the sides (|, -), +'s in the corners (+), and there is always a column of spaces to the left and right of the widest line of the message."

Some examples for what this function needs to do:

test that: border_msg('a') == '+---+\n| a |\n+---+\n'

test that: border_msg('hello') == '+-------+\n| hello |\n+-------+\n'

test that: border_msg("hi!\nhow are you?\ndrive safely!") == '+---------------+\n| hi! |\n| how are you? |\n| drive safely! |\n+---------------+\n'

I think it needs to print the above tests so that the words in the middle are surrounded by the "+------+ on the top and bottom and "|"'s on the sides.

Here is the code I have so far. I'm not sure where I would go from here.

def border_msg(msg):
    border_msg.split("\n")
    '%s'.format(msg)
    return border_msg(msg)
    print border_msg(msg)
martineau
  • 119,623
  • 25
  • 170
  • 301
famguy74
  • 27
  • 3
  • 11
  • fix indentation. And what about infinite recursion? – Jean-François Fabre Oct 10 '16 at 20:41
  • Just fixed the indentation and if you're talking about returning then printing I'm just not sure which one to use I guess either could work – famguy74 Oct 10 '16 at 20:43
  • 2
    famguy74: @Jean-François Fabre is talking about the fact that your `border_msg()` function calls itself unconditionally (so will never return). – martineau Oct 10 '16 at 20:58
  • Maybe it's just me, but I don't see how recursion helps in this case - as other mentioned, your code will not terminate, that's one issue, for one thing. Recursion is helpful, when there is some _sense_ of nesting. For example, if we needed to make __same multiple__ walls around the message. – quapka Oct 10 '16 at 21:24

3 Answers3

1
def border_msg(msg):
    msg_lines=msg.split('\n')
    max_length=max([len(line) for line in msg_lines])
    count = max_length +2 

    dash = "*"*count 
    print("*%s*" %dash)

    for line in msg_lines:
        half_dif=(max_length-len(line))
        if half_dif==0:
            print("* %s *"%line)
        else:
            print("* %s "%line+' '*half_dif+'*')    

    print("*%s*"%dash)



border_msg('first_line\nsecond_line\nthe_ultimate_longest_line') # without print
nisha
  • 11
  • 1
0

I've stitched up a little piece of code which implements the boxed messages. To be honest it's not the nicest piece of code, but it does the job and hopefully will help you to do it yourself (and better). For that purpose I've decided not to include comment, so you have to think that through for yourself. Maybe not the best educational method, but let's try it anyway:]

Code on Github.

from math import ceil, floor

def boxed_msg(msg):
    lines = msg.split('\n')
    max_length = max([len(line) for line in lines])
    horizontal = '+' + '-' * (max_length + 2) + '+\n'
    res = horizontal
    for l in lines:
        res += format_line(l, max_length)
    res += horizontal
    return res.strip()

def format_line(line, max_length):
    half_dif = (max_length - len(line)) / 2 # in Python 3.x float division
    return '| ' + ' ' * ceil(half_dif) + line + ' ' * floor(half_dif) + ' |\n'

print(boxed_msg('first_line\nsecond_line\nthe_ultimate_longest_line'))
# +---------------------------+
# |         first_line        |
# |        second_line        |
# | the_ultimate_longest_line |
# +---------------------------+
quapka
  • 2,799
  • 4
  • 21
  • 35
  • I don't state, that this is the _best possible_ answer, but could you comment the downvoting - or is it self-explanatory? – quapka Oct 10 '16 at 21:29
  • Thank you this is definitely the most comprehensive and helpful answer. I'm not sure who is downvoting all of these answers. A note to anyone reading this 3-6 PM is not a good time to post questions it's the only time I've gotten down votes all other parts of the day people are friendly and simply want to help – famguy74 Oct 10 '16 at 21:31
  • Maybe the problem is, that this site is not for doing homework for other people. I am not accusing you. But if it was, I provided it for you. But I allow myself to trust you, that you will do more, than just _copy_ the code. If not, this approach will get you in the future. Happy coding! – quapka Oct 10 '16 at 21:35
  • 1
    I can see where you're coming from. I am allowed to receive outside help as it is just an exercise/task and not a major assignment. Usually I come up with a solution myself and post it on stackoverflow for feedback but for this particular function it is really confusing – famguy74 Oct 10 '16 at 21:44
-1

Find out the length of your longest line N; (N+2) * '-' gives you the top and bottom borders. Before each line add a bar: '|'; pad each line with N - n spaces, where n is the length of that line. Append to each line a bar. Print in the correct order: top, line 1, line2, ..., line L, bottom.

Paul Brodersen
  • 11,221
  • 21
  • 38