简介
在这个实验中,你将学习如何判断 Python 中的循环是否已完成。这包括理解 for
循环的执行方式,并探索不同的方法来跟踪循环的完成情况。
你将从一个基本的 for
循环示例开始,遍历一个水果列表并打印每个水果。然后,你将学习如何使用标志变量来监控循环的进度。最后,你将了解如何结合使用 else
子句和循环,以便仅在循环正常结束后执行代码。
在这个实验中,你将学习如何判断 Python 中的循环是否已完成。这包括理解 for
循环的执行方式,并探索不同的方法来跟踪循环的完成情况。
你将从一个基本的 for
循环示例开始,遍历一个水果列表并打印每个水果。然后,你将学习如何使用标志变量来监控循环的进度。最后,你将了解如何结合使用 else
子句和循环,以便仅在循环正常结束后执行代码。
在这一步中,你将学习 Python 中 for
循环的执行方式。循环是基本的编程结构,它允许你多次重复执行一段代码。理解循环的工作原理对于编写高效且有效的程序至关重要。
让我们从一个简单的示例开始。使用 VS Code 编辑器在你的 ~/project
目录下创建一个名为 loop_example.py
的文件。
## loop_example.py
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
这段代码会遍历一个水果列表,并将每个水果打印到控制台。
要运行这个脚本,在 VS Code 中打开终端并执行以下命令:
python loop_example.py
你应该会看到以下输出:
apple
banana
cherry
让我们来分析一下代码中发生了什么:
fruits = ["apple", "banana", "cherry"]
:这行代码创建了一个名为 fruits
的列表,其中包含三个字符串:"apple"、"banana" 和 "cherry"。for fruit in fruits:
:这行代码开始了一个 for
循环。该循环会遍历 fruits
列表中的每个元素。在每次迭代中,当前元素会被赋值给变量 fruit
。print(fruit)
:这行代码位于循环内部。它会将 fruit
变量的值打印到控制台。循环会执行三次,列表中的每个水果各执行一次。在第一次迭代中,fruit
是 "apple",因此会打印出 "apple"。在第二次迭代中,fruit
是 "banana",因此会打印出 "banana"。在第三次迭代中,fruit
是 "cherry",因此会打印出 "cherry"。
现在,让我们修改脚本以包含每个水果的索引。将 loop_example.py
文件修改如下:
## loop_example.py
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(f"Index: {index}, Fruit: {fruit}")
在这个修改后的代码中,我们使用 enumerate()
函数来获取列表中每个元素的索引和值。
再次运行脚本:
python loop_example.py
你应该会看到以下输出:
Index: 0, Fruit: apple
Index: 1, Fruit: banana
Index: 2, Fruit: cherry
enumerate()
函数会返回一个由 (索引,元素) 对组成的序列。在每次迭代中,索引会被赋值给变量 index
,元素会被赋值给变量 fruit
。
理解循环的执行方式对于编写能够处理数据集合的程序至关重要。在接下来的步骤中,你将学习更多控制循环执行的高级技术。
在这一步中,你将学习如何使用标志变量来跟踪循环中任务的完成情况。标志变量是一个布尔变量(值为 True
或 False
),用于指示某个条件是否已满足。这是一种控制程序流程的常用技术。
让我们考虑这样一个场景:你想在一个列表中搜索一个特定的数字,并在找到它后立即停止循环。使用 VS Code 编辑器在你的 ~/project
目录下创建一个名为 flag_example.py
的文件。
## flag_example.py
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 5
found = False ## 初始化标志变量
for number in numbers:
print(f"Checking number: {number}")
if number == target:
found = True
print(f"Found the target: {target}")
break ## 退出循环
if found:
print("Target found in the list.")
else:
print("Target not found in the list.")
在这段代码中,我们将一个名为 found
的标志变量初始化为 False
。循环会遍历 numbers
列表。如果当前数字等于 target
,我们将 found
设置为 True
,打印一条消息,并使用 break
语句退出循环。循环结束后,我们检查 found
的值。如果为 True
,则打印一条消息表示已找到目标;否则,打印一条消息表示未找到目标。
要运行这个脚本,在 VS Code 中打开终端并执行以下命令:
python flag_example.py
你应该会看到以下输出:
Checking number: 1
Checking number: 2
Checking number: 3
Checking number: 4
Checking number: 5
Found the target: 5
Target found in the list.
注意,一旦找到目标数字(5),循环就会停止。break
语句会退出循环,防止进一步迭代。
现在,让我们修改脚本,搜索一个不在列表中的数字。将 target
变量改为 15:
## flag_example.py
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 15
found = False ## 初始化标志变量
for number in numbers:
print(f"Checking number: {number}")
if number == target:
found = True
print(f"Found the target: {target}")
break ## 退出循环
if found:
print("Target found in the list.")
else:
print("Target not found in the list.")
再次运行脚本:
python flag_example.py
你应该会看到以下输出:
Checking number: 1
Checking number: 2
Checking number: 3
Checking number: 4
Checking number: 5
Checking number: 6
Checking number: 7
Checking number: 8
Checking number: 9
Checking number: 10
Target not found in the list.
在这种情况下,循环会遍历整个列表,但没有找到目标数字。found
变量仍然为 False
,并打印出“Target not found in the list.”消息。
使用标志变量是一种简单而有效的方法,用于跟踪循环中任务的完成情况。这种技术在许多不同的场景中都很有用,例如在列表中搜索元素、验证用户输入或处理文件中的数据。
else
子句在这一步中,你将学习如何在 Python 的 for
循环中使用 else
子句。for
循环中的 else
子句会在循环正常完成时执行,也就是当循环没有被 break
语句终止时执行。这在执行那些仅在循环未找到特定元素或未遇到错误时才需要完成的操作时非常有用。
让我们回顾上一步中的示例,在该示例中我们在列表中搜索目标数字。我们可以使用 else
子句来在未找到目标数字时打印一条消息。使用 VS Code 编辑器在你的 ~/project
目录下创建一个名为 else_example.py
的文件。
## else_example.py
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 15
for number in numbers:
print(f"Checking number: {number}")
if number == target:
print(f"Found the target: {target}")
break
else:
print("Target not found in the list.")
在这段代码中,else
子句与 for
循环关联。如果循环在未找到 target
的情况下完成(即 break
语句未被执行),else
子句内的代码将被执行。
要运行这个脚本,在 VS Code 中打开终端并执行以下命令:
python else_example.py
你应该会看到以下输出:
Checking number: 1
Checking number: 2
Checking number: 3
Checking number: 4
Checking number: 5
Checking number: 6
Checking number: 7
Checking number: 8
Checking number: 9
Checking number: 10
Target not found in the list.
由于 target
(15)不在 numbers
列表中,循环正常完成,else
子句被执行,打印出“Target not found in the list.”消息。
现在,让我们修改脚本,将目标数字包含在列表中。将 target
变量改为 5:
## else_example.py
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 5
for number in numbers:
print(f"Checking number: {number}")
if number == target:
print(f"Found the target: {target}")
break
else:
print("Target not found in the list.")
再次运行脚本:
python else_example.py
你应该会看到以下输出:
Checking number: 1
Checking number: 2
Checking number: 3
Checking number: 4
Checking number: 5
Found the target: 5
在这种情况下,当找到 target
(5)时,循环被 break
语句终止。因此,else
子句不会被执行。
for
循环中的 else
子句提供了一种简洁的方式,用于在循环正常完成(未被 break
语句中断)时执行代码。这在处理那些仅在循环执行期间某个特定条件未满足时才需要执行特定操作的情况时非常有用。
在本次实验中,你学习了 Python 中 for
循环是如何通过遍历水果列表并打印每个水果及其索引来执行的。本实验展示了 for
循环的基本结构,包括使用 enumerate()
函数在迭代过程中访问列表中每个元素的索引和值。
最初的示例遍历了一个水果列表并打印每个水果。后续的修改使用 enumerate()
函数同时打印索引和水果,展示了如何在循环中访问元素的位置和值。