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,我已经编辑了我的代码。它现在正在工作,但不是我希望的那样。请告诉我使三角形变成倒置(向后)而不是规则三角形的方法。谢谢。
答案 0 :(得分:0)
您可以只使用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
* * * * *
* * * *
* * *
* *
*