def triangle():
n=int(input('Enter the number of lines for this triangle: '))
for i in range(1,n+1):
print ((n-i)*' '+i*'* ')
triangle()
根据my last question,我已经编辑了我的代码。它现在正在工作,但不是以我希望的方式。请告诉我一种方法,我可以使三角形倒置(向后),而不是像一个正三角形。谢谢。
发布于 2018-07-25 08:23:34
您可以只使用range(n,0,-1)
>>> triangle()
Enter the number of lines for this triangle: 5
* * * * *
* * * *
* * *
* *
*
更有趣的是:
def triangle(reverse=False):
n=int(input('Enter the number of lines for this triangle: '))
if reverse:
r = range(n,0,-1)
else:
r = range(1,n+1)
for i in r:
print ((n-i)*' '+i*'* ')
>>> triangle()
Enter the number of lines for this triangle: 5
*
* *
* * *
* * * *
* * * * *
>>> triangle(reverse=True)
Enter the number of lines for this triangle: 5
* * * * *
* * * *
* * *
* *
*
https://stackoverflow.com/questions/51509234
复制相似问题