def box_lines(lines, width):
topBottomRow = "┌" + "-" * width + "┐"
# bottomRow = "└" + "-" * length + "┘"
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]
def border_msg(msg, width):
return(box_lines(split_msg(msg, width), width))
print(border_msg("""♣
♣""", 20))
这就是我一直得到的......
┌--------------------┐
|♣ |
| |
| ♣|
┌--------------------┐
我不知道如何解决它。我正试图为我的oop二十一点游戏画一张牌。卡需要更长,我在代码中注释掉的底部符号需要用来制作一个完整的矩形。
答案 0 :(得分:0)
这是你想要实现的目标吗?
def border_msg(color, height, width):
card_content_width = width - 2
lines = ['┌' + '-' * card_content_width + '┐']
lines.append('|' + color + ' ' * (card_content_width - 1) + '|')
lines.extend(['|' + ' ' * card_content_width + '|' for i in range(height - 4)])
lines.append('|' + ' ' * (card_content_width - 1) + color + '|')
lines.append('└' + '-' * card_content_width + '┘')
return '\n'.join(lines)
print(border_msg("♣", 10, 20))
上面的代码为您提供了这样的卡片:
┌------------------┐
|♣ |
| |
| |
| |
| |
| |
| |
| ♣|
└------------------┘