Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串python的边框_Python - Fatal编程技术网

字符串python的边框

字符串python的边框,python,Python,编写一个以空格分隔的函数,该函数输出字符串s周围的空格和虚线边框 调用间隔(“Hello”)的示例代码将输出: --.-.-.-.- . . - Hello - . . -.-.-.-.-. 请帮我解决这个问题:D.我是编程新手,我正在努力学习这些东西。我没有任何编程经验,所以这对我来说是一个很大的挑战。谢谢大家 编程的关键是寻找模式,然后实现它们 定义您的需求: •必须具有固定间距字体 •顶部/底部的边框需要是文本长度+边距(空白

编写一个以空格分隔的函数,该函数输出字符串s周围的空格和虚线边框

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

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

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

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

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

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

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

确保如果你直接进入解释器(在命令行上),你需要插入适当的空格数,以免过早地从函数中返回。考虑<代码> H边沿=LINELINGINES//2 *”-“+:”[(LINELINGHANCE % 2)] /代码>,而不是一个费力的环大呼约翰。是楼层划分功能。x//y返回x/y的商。非常简洁的简化,但是,我想知道它是否只是在执行时简化为同一个程序集。@Justin:在示例输出的第一行中有输入错误吗?它以2个破折号开始。最后一行应该与第一行相同还是相反?