[英]Functions: printing blank lines (Python)
“如何像計算機科學家一樣思考:用Python學習”的第27頁寫道:
“作為一個練習,編寫一個名為nineLines的函數,它使用三條線來打印九條空行。你將如何打印二十七行?”
我寫:
def newLine():
print
def threeLines():
newLine()
newLine()
newLine()
def nineLines():
threeLines()
threeLines()
threeLines()
print 1
print nineLines()
print nineLines()
print nineLines()
print 2
結果是:
1
None
None
None
2
為什么那些“沒有”呢? 我想我不希望他們在那里。 我的推理是否正確? 謝謝。
你的推理是正確的,除了print
是多余的。 nineLines
無論如何都打印線。 您的print
語句打印的是函數的返回值,因為它沒有顯式返回任何內容是None
。
def threeLines():
for i in range(3):
print '\n'
def nineLines():
for i in range(3):
threeLines()
for i in range(3):
nineLines()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.