我正在尝试创建一个程序,允许我在 Python 程序中使用星号制作“金字塔”或“三角形”。我已经开始编写代码,但似乎无法弄清楚。
这是我想出的代码:
def triangle():
totalRows = int(eval(input("How big? ")))
for currentRows in range(1,totalRows+1):
for currentCol in range (1, currentRows+1):
print("*", end = " ")
triangle()
最终结果应该反射(reflect)这一点!
How big? 1
*
------------------------------------------------------
How big? 2
*
* *
------------------------------------------------------
How big? 3
*
* *
* * *
------------------------------------------------------
最佳答案
对 RParadox 的解决方案进行轻微修改以符合要求:
for i in range(totalRows + 1):
numWhite = totalRows - i
print ' ' * numWhite + '* ' * i
关于python - Python 中的星号三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13260642/