字符串python边框

时间:2010-10-30 03:33:47

标签: python

编写一个间隔的函数,用于输出空格和字符串s周围的短划线边框。

调用间隔(“Hello”)的示例代码将输出:

  --.-.-.-.-
.            .
-   Hello    -
.            .
  -.-.-.-.-.

请帮我解决这个问题:D。我是编程的新手,我正在努力学习这些东西。我没有任何编程经验,所以它对我来说是一个很大的挑战。谢谢大家!

1 个答案:

答案 0 :(得分:2)

编程的关键是寻找模式,然后实现它们。

定义您的要求:
•必须有固定空格字体
•顶部/底部的边框需要是文本+边距(空白区域)+边框
的长度 •文本在所有方向上必须有两个空格(垂直和水平)
•您想要交替的句号和连字符

def spaced(s):
    text = "hello"
    textLength = len(text)
    lineLength = textLength + 2 * (2 + 1)
    height = 5

    # at this point we know the first and fifth lines are the same and
    # we know the first and fourth are the same.  (reflected against the x-axis)

    hBorder = ""
    for c in range(lineLength):
        if c % 2:
            hBorder = hBorder + '.'
        else:
            hBorder = hBorder + '-'
    spacer = "." + " " * (lineLength - 2) + "."
    fancyText = "-  " + text + "  -"
    return (hBorder, spacer, fancyText, spacer, hBorder)

textTuple = spaced("hello world")
for line in textTuple:
    print line

请记住,您只能预测固定宽度字体的间距。如果您对上述功能有任何疑问,请在评论中提问。欢呼声。