tex周围的盒子有问题

2024-09-20 23:09:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这个问题已经被问了一次又一次;但是,我找不到其他人能够使用我正在使用的代码使它工作。我知道这个问题有几种解决办法。但我想知道为什么我的坏了。我很确定我错过了一些东西,这是盯着我的脸,但我现在的问题是,框不合并周围的文字,而是所有得到打印在一行。我附上了一个截图:https://imgur.com/a/2KviR

这是我的密码:

def Boxmaker():                                                     # Creating a function
    text = 'Arlidio'                                                # Defining Variable Text
    borders = text.splitlines()                                     # Spliting up 'Arlidio' into seperate lines
    boxwid = max(len(s) for s in borders)                           # Calculates length of longest word in 'borders'
    boxitems = ['|' + '¯' * boxwid + '|']                           # For the length of longest word creates box top border
    boxitems.append('|' + '_' * boxwid + '|')                       # Adds bottom border to existing variable 'boxitems'
    print(text.join(boxitems))                                      # Prints fused text

以及运行时结果:

|¯¯¯¯¯¯¯|Arlidio|_______|

Tags: of代码textinhttpslongestlengthword
2条回答

您需要使用new line,否则所有内容都将自动打印在同一行上,只是为了给您一个想法,请尝试以下操作:

text = ' Arlidio'
borders = text.splitlines()
boxwid = max(len(s) for s in borders)
boxitems = ['|' + '¯' * boxwid + '|'+ '\n']
boxitems.append('\n|' + '_' * boxwid + '|')
print(text.join(boxitems)

这里基本上有三根弦,它们是:

"|¯¯¯¯¯¯¯|"  # top border
"Arlidio"    # text
"|_______|"  # bottom border

听起来你想做的是把这些按顺序组合起来,用新行分隔每一个。可能是这样的:

"\n".join([topborder, text, bottomborder])

请记住str.join将对象视为参数的分隔符,例如",".join(["Hello", " World!"]) == "Hello, World!"。因此,您的代码用文本"Arlidio"分隔上下边框,但不会在任何地方添加任何换行符。你知道吗

相关问题 更多 >

    热门问题