【问题标题】:Draw a box around message line [duplicate]在消息行周围画一个框[重复]
【发布时间】:2016-10-11 01:40:17
【问题描述】:

我之前问过这个问题,但我收到的答案最终无法正常工作,所以我完全重新开始。我现在有更多开发的代码,但仍然无法弄清楚出了什么问题以及如何将 a 和 hello 包围在这样的框中:

最初的问题是:给定一条可能包含多行的消息,利用 split() 函数来识别各个行,并使用我们在解决方案中学到的任何一种格式化方法来创建字符串,打印时,围绕消息的行绘制一个框,全部居中。该框在两侧使用竖线和短划线(|、-),在角落使用加号(+),并且在消息的最宽行的左右始终有一列空格。所有线条都居中。

这是我想出的代码。我认为它在正确的轨道上,但我遇到了错误,尤其是 ver 函数

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"))

【问题讨论】:

  • 顺便说一句:count = len(msg)
  • 感谢我刚刚修改的提示
  • {:^'count'} ???它不是 Bash - 你不能使用 ' ' 插入值。
  • 我应该把它改成 {:^count} 吗?
  • 我想你的同学already beat you to the punch

标签: python draw message box


【解决方案1】:

要在字符串格式中使用count 值,您需要

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

或使用名称 - 这样对您来说更具可读性

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

但你也可以这样做

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

你可以在字符串中添加空格

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

你的代码

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

或者没有print内部函数

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

【讨论】:

  • 谢谢,我认为这是最好的解决方案
【解决方案2】:

这是在某物周围放置一个盒子的可能解决方案:

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)

它需要正确分割的行:

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

box_linessplit_msg 放在一起,我们得到:

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

我们可以按如下方式使用它:

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

哪些输出

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

【讨论】:

  • 您的解决方案运行良好,但生成“-”边框存在问题。它说border_msg(hello):预期+-----+但得到+-----+。我如何让它为边界再生成两个“-”?
  • @famguy74 好了。这比我想象的要难。
  • 谢谢我选择了其他人的答案作为最佳答案,因为他们在一个函数中完成了它,尽管我可能会将其更改为您的,因为格式存在问题我也会尝试使用您的答案跨度>
猜你喜欢
  • 2017-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多