一 -14 python (基础)while 语句嵌套

print 函数的使用做一个增强

  • print("*",end="" ) 不换行
  • print("") 单纯的换行
  • \n 换行符

打印*

循环嵌套的核心思想:外层的循环执行一次,内层循环会执行若干次

i = 1
while i <= 5:
    print("*" * i)
    i +=1

row = 1
while row <= 5:
    col = 1
    while col <= row
        print("*", end = "")
        col += 1
    print(") #换行符
    row += 1

乘法口诀

row = 1
while row <= 5:
    col = 1
    while col <= row
        print("%d * %d = %d" %(col,row,(col * row)), end = "\t")
        col += 1
    print()
    row += 1

你可能感兴趣的:(一 -14 python (基础)while 语句嵌套)