“笨办法”学Python 3 ——练习 33 While 循环
练习33 源代码
i = 0 #初始值
numbers = [] #用于存储的空列表
while i < 6 : #当i小于6时,布尔值为True,运行以下代码块
print(f"At the top i is {i}") #打印while循环开头的变量
numbers.append(i)
i = i + 1
print("Numbers now: ",numbers) #打印while循环结尾值
print(f"At the bottom i is {i}") #打印while循环结尾值
print("The numbers: ")
for num in numbers: #for循环
print(num)
输出结果
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
0
1
2
3
4
5
知识点:
1.while循环与for循环的区别?
Python中for循环和while循环本质上是没有区别的,都是去检查一个布尔表达式的真假,但是在实际应用上,针对性不太一样。
while循环适用于未知循环次数的循环,for循环适用于已知循环次数的循环 。
for主要应用在遍历中;而while循环很少进行遍历使用(语句过多,没有for方便),while主要用于判断符合条件下循环。
2. while循环运用
使用 while 循环,只要条件为真,我们就可以执行一组语句。
例如:
只要 i 小于 7,打印 i:
i = 1
while i < 7:
print(i)
i += 1
输出:
1
2
3
4
5
6
附加练习
1.把这个 while-loop 转换成一个你可以调用的函数,然后用一个变量替代 i < 6 里面的 6。&2.用这个函数重新写一下这个脚本,试试不同的数值。
代码如下:
def while_loop(a):
numbers = [] #用于存储的空列表
i = 0
while i < a : #当i小于a时,布尔值为True,运行以下代码块
print(f"At the top i is {i}") #打印while循环开头的变量
numbers.append(i)
i = i + 1
print("Numbers now: ",numbers) #打印while循环结尾值
print(f"At the bottom i is {i}") #打印while循环结尾值
return numbers #返回number值,return与while平齐,不然无法执行while循环
a = int(input("The top of numbers is:"))
numbers = while_loop(a)
print("The numbers: ")
for num in numbers: #for循环
print(num)
输出结果:
当变量a=8时:
The top of numbers is:8
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
At the top i is 6
Numbers now: [0, 1, 2, 3, 4, 5, 6]
At the bottom i is 7
At the top i is 7
Numbers now: [0, 1, 2, 3, 4, 5, 6, 7]
At the bottom i is 8
The numbers:
0
1
2
3
4
5
6
7
3. 再增加一个变量给这个函数的参数,然后改变第 8 行的 +1,让它增加的值与之前不同。&4.用这个函数重新写这个脚本,看看会产生什么样的效果。
输入代码:
def while_loop1(a,n): #定义2个参数,a为最大不超过的数值,n为步长
numbers = [] #用于存储的空列表
i = 0
while i < a : #当i小于a时,布尔值为True,运行以下代码块
print(f"At the top i is {i}") #打印while循环开头的变量
numbers.append(i)
i = i + n #每次循环i的步长为n
print("Numbers now: ",numbers) #打印while循环结尾值
print(f"At the bottom i is {i}") #打印while循环结尾值
return numbers #返回number值,return与while平齐,不然无法执行while循环
b = int(input("The top of numbers is: "))
m = int(input("The step size is: "))
numbers = while_loop1(b,m)
for num in numbers:
print(num)
输出结果:
number不超过10,第八行+1,更新为+2:
The top of numbers is: 10
The step size is: 2
At the top i is 0
Numbers now: [0]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 2]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 2, 4]
At the bottom i is 6
At the top i is 6
Numbers now: [0, 2, 4, 6]
At the bottom i is 8
At the top i is 8
Numbers now: [0, 2, 4, 6, 8]
At the bottom i is 10
0
2
4
6
8
5.用 for-loop 和 range 写这个脚本。你还需要中间的增加值吗?如果不去掉这个增加值会发生什么?
用for—loop不需要使用中间增加值,代码如下:
numbers = []
for i in range(0,6):
print(f"At the top i is {i}") #打印while循环开头的变量
numbers.append(i)
print("Numbers now: ",numbers) #打印while循环结尾值
print("The numbers: ")
for num in numbers: #for循环
print(num)
输出结果:
At the top i is 0
Numbers now: [0]
At the top i is 1
Numbers now: [0, 1]
At the top i is 2
Numbers now: [0, 1, 2]
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
The numbers:
0
1
2
3
4
5
若不去掉增加值,numbers列表结果不变,循环时并不会引用i=i+1的值,但是At the bottom i is
的值是i=i+1,代码如下:
numbers = []
for i in range(0,6):
print(f"At the top i is {i}") #打印while循环开头的变量
numbers.append(i)
print("Numbers now: ",numbers) #打印while循环结尾值
#若不去掉增加值
i = i+1
print(f"At the bottom i is {i}") #打印while循环结尾值
print("The numbers: ")
for num in numbers: #for循环
print(num)
输出结果:
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
0
1
2
3
4
5
常见问题
1. for-loop 和 while-loop 的区别是什么? for-loop 只能迭代(循环)一些东西的集合,而 while-loop 能够迭代(循环)任何类型的东西。不过,while-loop 很难用对,而你通常能够用 for-loop 完成很多事情。(同知识点1)