方法一:
# 使用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