效果图
分析行
一共有几行?
九行
分析列
列有什么规律
第一行,会有一列
第二行,会有二列
第五行,会有五列
第n行,会有n列
写个函数打印纵向(行)
code
def print_row():
# 打印九行
row = 1
while row <= 9:
print('当前是第', row, '行')
row = row + 1
print_row()
写个函数打印横向(列)
注意:列是动态的
def print_col(num):
i = 1
while i <= num:
print("*", end="\t")
i = i+1
print_col(3)
行与列联合起来
第一阶段
# 动态的打印列
def print_col(num):
i = 1
while i <= num:
print("*", end="\t")
i = i+1
# 行的打印
def print_row():
# 打印九行
row = 1
while row <= 9:
# print('当前是第', row, '行')
print(row)
print_col(row)
row = row + 1
print_row()
最终阶段
# 动态的打印列
def print_col(num):
i = 1
while i <= num:
# print(i,num,end=' ')
res = i * num
con = str(i) + "*" + str(num) + "=" + str(res)
print(con, end="\t")
i = i + 1
# 行的打印
def print_row():
# 打印九行
row = 1
while row <= 9:
# print('当前是第', row, '行')
print_col(row)
row = row + 1
print()
print_row()
# ctrl + alt + L