开发者学堂课程【Python入门 2020年版:嵌套打印矩形】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/639/detail/10270
嵌套打印矩形
内容介绍:
一、 while 嵌套的格式
二、练习
三、总结
一、 while 嵌套循环的格式
while 条件1:
外循环代码块
While条件2:
内循环代码块
内循环一旦开始,则要执行到条件2不满足,才会继续执行外循环的代码
i = 0 #内外循环的控制变量不能一样
while I < 5:
j = 0 #内循环的控制变量必须要在外循环里初始化
while j < 5:
print(‘j = %d’ % j)
j +=1
print(‘I = %d’ % i)
I +=1
二、练习:
打印如下图形
打印九九乘法表。
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=3б
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
打印五个星星:
print(‘*’ * 5)
运行结果:
C:\Users\chris\AppData\Local\Programs\Python\Python37\python.exe C:/Users/chris/Desktop/Python 基础/Day04-流程*****Process finished with exit code 0
打印5行:
i = 0
while i < 5:
print(‘*’ * 5, end=’\n’)
#表示换行
i +=1
运行结果:
C:\Users\chris\AppData\Local\Programs\Python\Python37\python.exe C:/Users/chris/Desktop/Python 基础/Day04-流程
*****
*****
*****
*****
Process finished with exit code 0
因不满足要求
i =0
while i < 5:
i+=1
print(i)
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程1
2
3
4
5Process finished with exit code 0
#Python 里可以使用一层循环直接打印三角形
在 Python 里支持乘法运算。
i = 0
while i < 5
i +=1
print(i * ‘*’)
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程
*
**
***
****
*****
打印一个星星:
print(“*”)
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程*Process finished with exit code 0
print(“*”)
print(‘*’)
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程
*
*
Process finished with exit code 0
变成两行的原因是打印完星星后会换行。
print(“*”
,end=’ ‘)
#end默认换行。
print(‘*’)
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程
* *
Process finished with exit code 0
打印完第一个星星后,不换行,紧跟4个空格,下一个星星就会紧跟4个空格。
print(“*”
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’,end=’ ‘)
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程
* * *
Process finished with exit code 0
打印五个星星:
print(“*”
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’,end=’ ‘)
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程
* * * * *
Process finished with exit code 0
print(“*”
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’
,end=’ ‘)
print
()
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程
* * * * *
Process finished with exit code 0
所以 print 是用来换行的。
print(“*”
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’
,end=’ ‘)
print
()
print(“*”
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’
,end=’ ‘)
print(‘*’
,end=’ ‘)
运行结果:
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程
* * * * *
* * * * *
Process finished with exit code 0
进行优化:把它放入循环里。
i = 0
while i < 5:
i +=1
print(“*”,end=’ ‘)
print()
打印五个星星换行。
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程
* * * * *
Process finished with exit code 0
#这一大段代码,是用来打印五行五列星星的
j = 0
while j < 5:
j += 1
#本段代码是打印五个星星并且换行
i =0
while I < 5
i += 1
print(“*”,end=’ ‘)
#打印一个星星,不换行
print()
#用来换行
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Process finished with exit code 0
打印十行星星:
j = 0
while j < 10
:#注意改这里
j += 1
i =0
while i < 5
:
i += 1
print(“*”,end=’ ‘)
print()
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程
* * * * *
* * * * *
* * * * *
* * * * *
Process finished with exit code 0
打印八个星星:
j = 0
while j < 10:
j += 1
i =0
while i < 8
:
#注意改这里
i += 1
print(“*”,end=’ ‘)
print()
运行结果:
C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python 基础/Day04-流程
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
Process finished with exit code 0
三、总结
外循环用来控制行数;内循环用来控制每一行的列数