2017-03-29 59 views
1

做出0

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


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

這裏是當我運行該程序會發生什麼:在哪裏添加換行符

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

我想添加換行符在中部和高度之間,使它看起來如0高度爲5的形狀。

回答

2

嘗試將middle = ("* *")更改爲middle = "* *\n"。並且不要在構建結果中使用最後的"\n"

+0

並使結果爲: 'result = toporbottom +「\ n」+(middle)* h八+ toporbottom' – maccinza

+1

@maccinza我加了。希望OP在看到輸出結果時能夠明白這一點。 –

0

可能:結果= toporbottom + 「\ n」 +(中間+ 「\ n」)*高+ toporbottom

0

你只需要改變:

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

and

  • 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) 

輸出:

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

P.S:現在你應該在middletoporbottom改變硬編碼符號"*"的功能參數symbol

0

你只需要改變本代碼(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) 

這應該做的伎倆

+0

請使用[編輯]鏈接來解釋此代碼的工作原理,而不只是給出代碼,因爲解釋更有可能幫助未來的讀者。另見[回答]。 [源(http://stackoverflow.com/users/5244995) –