方法一:
# 使用while,完成圖形的輸出
i = 1
k = 5
while i < 6:
j = 1
while j <= i:
print('*', end=' ')
j += 1
print('\n')
i += 1
while k > 0:
j = 1
while j < k:
print('*', end=' ')
j += 1
print('\n')
k -= 1
方法二:
# 最大星星數
my_num = 5
# 循環次數
row_index = 1
while row_index <= 2 * my_num - 1:
# 定義一個列數
col_index = 1
# 完成列印上半部分
if row_index <= my_num:
while col_index <= row_index:
print("*", end="")
col_index += 1
else:
# 完成列印下半部分
while col_index <= 2 * my_num - row_index:
print("*", end="")
col_index += 1
print()
row_index += 1