我正在使用嵌套循環創建一個反轉三角形,並以任何字符量輸入。例如,如果輸入8,我的三角應該是這個樣子使用嵌套循環繪製三角形
xxxxxxxx
xxxxxxx
xxxxxx
xxxxx
xxxx
xxx
xx
x
我的代碼目前包括以下什麼,但輸出是不是我所期待的。
row = 1
while row <= size:
# Output a single row
col = size - row + 1
while col <= size:
# Output the drawing character
print(end=' ')
# The next column number
col = col + 1
col = 0
while col <= size - row:
print(drawingChar, end=' ')
col = col + 1
row = row + 1
print()
print()
輸出:
x x x x x x x x
x x x x x x x
x x x x x x
x x x x x
x x x x
x x x
x x
x
我敢肯定,我搞砸了次要的東西與我的<= size
或山坳地方。所有的輸入是讚賞。
的[反向倒置在python星號三角形]可能重複(HTTP:/ /stackoverflow.com/questions/19034814/reverse-upside-down-asterisk-triangle-in-python) – davedwards
@downshift我看到那篇文章,並從這個答案中拿了一些東西,但即時通訊仍然把它指向等邊三角形 – Astonishing
好的抱歉,一個沒有幫助。嘗試在第二個'while'循環中從'end'參數中刪除字符:'print(drawingChar,end ='')' –
davedwards