在消息行周围画一个框
Draw a box around message line
我之前问过这个问题,但我收到的答案最终无法正常工作,所以我完全重新开始。我现在有了更多开发的代码,但仍然无法弄清楚出了什么问题以及如何像这样在框中包围 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
值,您需要
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
这里有一个可能的解决方案,可以在某物周围放置一个盒子:
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_lines
和 split_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 |
+--------------------+
我之前问过这个问题,但我收到的答案最终无法正常工作,所以我完全重新开始。我现在有了更多开发的代码,但仍然无法弄清楚出了什么问题以及如何像这样在框中包围 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
值,您需要
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
这里有一个可能的解决方案,可以在某物周围放置一个盒子:
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_lines
和 split_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 |
+--------------------+