while循环嵌套
循环里面包含循环,即为循环嵌套
- print函数知识扩展
print函数默认输出一条语句后自动换行
n = 1
i = 1
while i <= 5:
print("*" * n)
n += 1
i += 1
默认是自动换行的,如上例会输出数量递增的5排星星
如果希望print结束后不自动换行,可以在语句末尾增加end=(“”),引号内可以加入你想要显示的内容
print (“*”,end=“”) 这样加上end=““后就会连续输出,不换行
print(””)这条代码就是单纯执行换行
通过循环嵌套输出数量递增的5排星星
row = 1
while row <= 5:
col = 1
while col <= row:
print("*",end="")
col += 1
print("")
row += 1
循环嵌套示例——九九乘法表
row = 1
while row <= 9:
col = 1
while col <= row:
print("%d x %d = %d" % (col,row,col * row),end="\t") #加上\t,输出时对齐,版面美观
col += 1
print("")
row += 1