1 回答

TA貢獻1806條經驗 獲得超8個贊
在你的循環中,你不斷增加1,直到達到 6,并且不斷增加2 sindex的長度,這樣它就會以 6*2-1 s 結束。那么,你嘗試過扭轉局面嗎?chararrowarrows
size從*2-1的字符串開始arrow,設置index為size,每次迭代遞減index,并繼續直到達到 0,并每次刪除兩個字符char:
def down_arrow(arrow):
size = 6
index = size
char = arrow * (size * 2 - 1)
while index > 0:
spaces = " " * (6 - index)
print(spaces + char)
char = char[2:]
index -= 1
down_arrow('.')
請注意,我嘗試堅持您最初編寫的方式,有更多最佳方法可以實現此目的。
例如,雖然仍然只打印這種類型的箭頭,但此函數可以同時執行以下操作:
def arrow(ch, size, up=True):
for i in range(1, size + 1) if up else range(size, 0, -1):
print(' ' * (size - i) + ch * (2 * i - 1))
arrow('.', 6)
arrow('.', 6, up=False)
添加回答
舉報