-1
python 3.5 Spyder
文本三角形在文檔測試
不工作由於某種原因,我的輸出包括印而不是進行實際的新行\ n個字符。我認爲我\ n的位置不正確,我試過將它放在多個位置,但我沒有看到任何改進。
對於例如:text_triangle(5),得到:
X\\nXX\\nXXX\\nXXXX\\nXXXXX\\nXXXXX\\nXXXX\\nXXX\\nXX\\nX\\n
python文檔使用反斜線但這並不會改變輸出時使用原始文檔字符串提及。我覺得我的代碼確實返回了正確數量的字符,但如何讓新行發生對我來說是個謎。
def text_triangle(height):
"""(int) -> string
Returns a text version of an isosceles triangle built from 'X' characters
with height copies of the X in the longest line. The triangle is returned
in a multiline string. The string is empty if height <= 0.
For instance, if we call print(text_triangle(5))it will print the following:
X
XX
XXX
XXXX
XXXXX
XXXX
XXX
XX
X
>>> text_triangle(0)
''
>>> text_triangle(1)
X
>>> text_triangle(3)
X
XX
XXX
XX
X
>>> text_triangle(5)
X
XX
XXX
XXXX
XXXXX
XXXX
XXX
XX
X
"""
if(height <= 0):
return ''
else:
out_string = ''
for i in range(0, height):
out_string += 'X' * (i+1) + '\\n'
for i in range(height, 0, -1):
out_string += 'X' * i + '\\n'
return out_string
if __name__ == '__main__':
import doctest
print(doctest.testmod(verbose = True,
optionflags = doctest.NORMALIZE_WHITESPACE))