消息周围的绘图框 [英] Drawing box around message

查看:92
本文介绍了消息周围的绘图框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行我不知道的Python任务.它是3个功能中的最后一个,而前2个功能比该功能更容易编程.说明是 给出一条可能包含多行的消息,利用split()函数标识各行,并使用format()函数,以便在打印时在消息的各行周围绘制一个居中的框,所有框居中.Box使用竖线和横线;在横线(|,-)处加破折号,在角(+)处加号,并且在消息的最宽行的左侧和右侧始终会有一列空格."

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:

测试以下内容:border_msg('a')=='+ --- + \ n |一个| \ n + --- + \ n'

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

测试以下内容:border_msg('hello')=='+ ------- + \ n |你好| \ n + ------- + \ n'

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

测试以下内容:border_msg(嗨!\ n您好吗?\ n安全开车!")=='+ --------------- + \ n |你好! | \ n |你好吗? | \ n |安全驾驶! | \ 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)

推荐答案

我整理了一些代码来实现盒装消息.老实说,这不是最好的代码,但是它可以完成工作,希望可以帮助您自己(甚至更好)做到这一点.为此,我决定不添加评论,因此您必须自己考虑一下.也许不是最好的教育方法,但是还是让我们尝试一下:]

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:]

Github 上的代码.

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 |
# +---------------------------+

这篇关于消息周围的绘图框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆