添加换行符的位置



make a 0

def number0(width, height, symbol):
    toporbottom  = ("*"*5)
    middle = ("*   *")
    result = toporbottom + "n" + (middle) * height + "n" + toporbottom
    return result

result = number0(5, 5, "*")
print (result)

以下是我运行程序时发生的情况:

*****
*   **   **   **   **   *
*****

我想在中间和高度之间添加换行符,使其看起来像高度为 5 的 0 形状。

尝试将middle = ("* *")更改为middle = "* *n"。并且不要在构建结果中使用最后"n"

也许:结果 = toporbottom + "" +

(中间 + ""( * height + toporbottom

你只需要改变:

  • middle = ("* *")
  • middle = ("* *n")

  • result = toporbottom + "n" + (middle) * height + "n" + toporbottom
  • result = toporbottom + "n" + (middle) * height + toporbottom .

法典:

def number0(width, height, symbol):
    toporbottom  = ("*"*5)
    middle = ("*   *n")
    result = toporbottom + "n" + (middle) * height + toporbottom
    return result

result = number0(5, 5, "*")
print (result)

输出:

*****
*   *
*   *
*   *
*   *
*   *
*****

PS:现在您应该将middletoporbottom中的硬编码符号"*"更改为函数参数symbol

您只需将当前代码 (1( 更改为 (2(:

1) middle = ("*   *")
   result = toporbottom + "n" + (middle) * height + "n" + toporbottom  
2) middle = "*   *n"
   result = toporbottom + "n" + (middle) * height + toporbottom  

因此,修改后的代码将是:

def number0(width, height, symbol):
    toporbottom  = ("*"*5)
    middle = "*   *n"
    result = toporbottom + "n" + (middle) * height  + toporbottom
    return result

result = number0(5, 5, "*")
print (result)

这应该可以解决问题

最新更新