在Python/仅使用嵌套循环创建对角线



好吧,在过去的几个小时里,我一直盯着屏幕试图解决这个问题。我必须创建一条对角线,它可以有任何字符。

需要看起来像这样:

*
  *
    *

这是我现在拥有的代码。

# Draw a Diagonal
row = 1
while row <= size:
# Output a single row
col = 1
while col <= row:
    # Output the drawing character
    print()
    # The next column number
    col = col + 1
# Output a newline to end the row
print(end=drawingChar)
# The next row number
row = row + 1
print()

而且它不会靠近对角线!可能需要一些见解,谢谢你的帮助。我必须使用嵌套循环。我在搜索中看到了其他几个类似的问题,但它们不适用于我的任务。


您可以使用一个简单的for循环来完成此操作:

>>> for i in range(5):
    print(' '*i, end = '*n')

*
 *
  *
   *
    *

最新更新