我知道这个问题已经被问了一次又一次;但是,我找不到其他人能够使用我正在使用的代码使它工作。我知道这个问题有几种解决办法。但我想知道为什么我的坏了。我很确定我错过了一些东西,这是盯着我的脸,但我现在的问题是,框不合并周围的文字,而是所有得到打印在一行。我附上了一个截图: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|_______|
您需要使用
new line
,否则所有内容都将自动打印在同一行上,只是为了给您一个想法,请尝试以下操作:这里基本上有三根弦,它们是:
听起来你想做的是把这些按顺序组合起来,用新行分隔每一个。可能是这样的:
请记住
str.join
将对象视为参数的分隔符,例如",".join(["Hello", " World!"]) == "Hello, World!"
。因此,您的代码用文本"Arlidio"
分隔上下边框,但不会在任何地方添加任何换行符。你知道吗相关问题 更多 >
编程相关推荐