介绍 if 语句
介绍 if 语句现在我们要学习如何告诉计算机做出决策,并且只在某些条件下运行代码。为此,我们需要一种新的值类型。你已经见过 数字和字符串,现在来认识一下 布尔值。布尔值只有两个: True 和 False。试试这个程序:condition = True
print(condition)
condition = False
print(condition)
布尔值是用于 if 语句(有时也称为 条件语句)中的。
这里有一个简单的例子供你运行:
if True:
print('This gets printed')
if False:
print('This does not')
注意第一个 if 语句中的代码运行了,但第二个没有。
一般来说,if 语句看起来像这样:
if <condition>:
<body>
其中 <condition> 是任何计算为布尔值的表达式,而 <body> 是一个 缩进的 语句列表 可以包含一个或多个语句。这个结构与 for 循环非常相似。注意冒号 (:),这是必不可少的。
当计算机看到 if <condition>: 时,它会检查 <condition> 是否为 True。如果是,它就会执行主体。 如果不是,它会跳过并继续执行程序的其余部分。
这是一个更有趣的例子供你运行:
sentence = 'Hello World'
excited = True
if excited:
sentence += '!'
print(sentence)
(记住 sentence += '!' 意味着 sentence = sentence + '!')
将 excited = True 改为 excited = False,然后再次运行程序,看看有什么不同。
现在是练习时间。修改上面的程序,添加一个额外的 布尔参数 confused,程序应该像这样开始:
sentence = 'Hello World'
excited = False
confused = True
(sentence 可以是任何字符串,两个布尔值可以是 True 或 False)
当 confused 为真时,打印的句子应该在末尾添加一个问号。 如果 confused 和 excited 都为真,句子应该以 !? 结束。
Hi,鱼油!
if 语句是让程序根据条件做出决策的关键工具。它的核心依赖一种新数据类型——布尔值(bool),只有两个可能:True 和 False。
if 语句的基本结构如下:
if 条件:
缩进的代码块(可以是多行)
当 条件 为 True 时,缩进的代码块会执行;为 False 时则跳过。
来看一个简单例子:
if True:
print('这段会被打印')
if False:
print('这段不会')
运行后发现只有第一段打印了。
更实用的例子:
sentence = 'Hello World'
excited = True
if excited:
sentence += '!'
print(sentence)
这里如果 excited 为 True,字符串末尾会加上 !。你可以尝试把 excited = True 改成 excited = False 看看区别。
记住:条件必须是能计算出布尔值的表达式,比如 True、False、比较表达式(如 x > 5)等。冒号 : 和缩进绝对不能漏掉哦!
——FishC计算机教育专家
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 注意 if 语句的主体(4 行)像往常一样缩进,而 for 循环的主体(1 行)在每行额外缩进了 4 个空格,以显示这些行在 for 循环内。你可以通过查看缩进来了解程序的整体结构。
或者,你可以在 for 循环内放一个 if:
注意 if 语句的主体(4 行)像往常一样缩进,而 for 循环的主体(1 行)在每行额外缩进了 4 个空格,以显示这些行在 for 循环内。你可以通过查看缩进来了解程序的整体结构。
或者,你可以在 for 循环内放一个 if: ling830 发表于 2026-07-24 16:40
注意 if 语句的主体(4 行)像往常一样缩进,而 for...
当然!嵌套是编程中的常见模式,可以在循环内部使用 if 语句进一步筛选或处理数据。
理解缩进是关键:外层 for 循环的代码缩进一次,内层 if 语句的代码则再缩进一次(通常4个空格)。
示例:
words = ['apple', 'banana', 'cherry']
for word in words:
if 'a' in word:
print(word + ' contains letter a')
else:
print(word + ' does not contain a')
这里 for 循环遍历列表中的每个单词,内部 if 语句检查单词是否包含字母 'a',并分别打印不同信息。缩进明确表示了层次关系:if 语句属于 for 循环的一部分。
当你需要为每个元素做条件判断时,这种嵌套非常有用。记住:所有内层代码必须正确缩进,否则 Python 会报错。
——FishC计算机教育专家
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 这两个程序的结果完全相同。然而,第一个程序更高效,因为它只在需要时才遍历字符串,因为当 excited = False 时,什么都不会改变。 ling830 发表于 2026-07-24 16:40
这两个程序的结果完全相同。然而,第一个程序更高效, ...
当然,你的理解完全正确!当 excited = False 时,第一个程序直接跳过了整个字符串拼接操作,而第二个程序(假设你指的是不判断直接操作再回退的方案)会先执行操作再判断,造成时间与内存的浪费。所以优先使用条件判断,仅在需要时才执行代码,这是提高效率的常见技巧。
——FishC计算机教育专家
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 使用 snoop 理解程序
使用 snoop 理解程序sentence = 'Hello World'
include = False
new_sentence = ''
for char in sentence:
if include:
new_sentence += char
include = True
print(new_sentence)
程序开始变得复杂了! 是时候引入一个新工具来帮助你理解程序了。点击 snoop 按钮来运行相同的程序,同时显示发生了什么。 ling830 发表于 2026-07-24 16:43
使用 snoop 理解程序
使用 snoop 理解程序sentence = ...
当程序逻辑变复杂时,手动追踪变量状态容易出错。snoop 是一种可视化调试工具,它会逐步展示代码执行的过程,包括每一行运行后变量的值变化,帮助你直观理解程序的行为。
下面是你给的程序的snoop执行流程示意(假设用snoop运行):
sentence = 'Hello World'
include = False
new_sentence = ''
# 进入 for 循环,遍历 sentence 中的每个字符
# 第1次循环:char = 'H'
# if include: 此时 include=False,跳过
# include = True
# 第2次循环:char = 'e'
# if include: 现在 include=True,执行 new_sentence += 'e'
# include = True(不变)
# 第3次循环:char = 'l'
# if include: True,添加 'l'
# ...
# 循环结束后,new_sentence = 'ello World'(注意第一个字符'H'没有被添加)
snoop 会高亮当前执行行,并在侧边栏显示每个变量的当前值,让你看到 include 从 False 变成 True 后,后续所有字符都被添加,唯独跳过了首个字符。这就是程序最终输出 ello World 的原因。
尝试自己用snoop跑一遍,你会亲眼看到每一步的变化,从而真正理解代码逻辑。记住:工具是理解程序的好帮手,但最终要靠你分析流程。
——FishC计算机教育专家
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2026-7-24 16:43
当程序逻辑变复杂时,手动追踪变量状态容易出错。snoop 是一种可视化调试工具,它会逐步展示代码执行的 ...
1 | sentence = 'Hello World'
3 | include = False
4 | new_sentence = ''
5 | for char in sentence:
...... char = 'H'
6 | if include:
8 | include = True
5 | for char in sentence:
...... char = 'e'
6 | if include:
7 | new_sentence += char
.............. new_sentence = 'e'
8 | include = True
5 | for char in sentence:
...... char = 'l'
6 | if include:
7 | new_sentence += char
.............. new_sentence = 'el'
8 | include = True
5 | for char in sentence:
6 | if include:
7 | new_sentence += char
.............. new_sentence = 'ell'
8 | include = True
5 | for char in sentence:
...... char = 'o'
6 | if include:
7 | new_sentence += char
.............. new_sentence = 'ello'
8 | include = True
5 | for char in sentence:
...... char = ' '
6 | if include:
7 | new_sentence += char
.............. new_sentence = 'ello '
8 | include = True
5 | for char in sentence:
...... char = 'W'
6 | if include:
7 | new_sentence += char
.............. new_sentence = 'ello W'
8 | include = True
5 | for char in sentence:
...... char = 'o'
6 | if include:
7 | new_sentence += char
.............. new_sentence = 'ello Wo'
8 | include = True
5 | for char in sentence:
...... char = 'r'
6 | if include:
7 | new_sentence += char
.............. new_sentence = 'ello Wor'
8 | include = True
5 | for char in sentence:
...... char = 'l'
6 | if include:
7 | new_sentence += char
.............. new_sentence = 'ello Worl'
8 | include = True
5 | for char in sentence:
...... char = 'd'
6 | if include:
7 | new_sentence += char
.............. new_sentence = 'ello World'
8 | include = True
5 | for char in sentence:
10 | print(new_sentence)
ello World ling830 发表于 2026-07-24 16:44
FishC 发表于 2026-7-24 16:43
当程序逻辑变复杂时, ...
你展示的snoop输出完全正确!它清晰展示了每一步的执行情况:
- 初始时 `include = False`,循环第一个字符 `'H'` 时,`if include` 不执行,直接跳过拼接。
- 然后 `include` 被设为 `True`,从此之后每一个字符都会被添加到 `new_sentence` 中。
- 最终 `new_sentence` 结果是 `'ello World'`(跳过了首字符 `'H'`)。
这正是程序的核心逻辑:利用一个布尔标志 `include` 来控制从第二个字符开始复制。snoop 通过实时显示变量值和执行行,让你直观看到“条件判断”与“标志变化”的配合过程,是理解复杂循环与条件嵌套的绝佳助手。
——FishC计算机教育专家
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 让我们一起看看 snoop 正在向你展示什么。 一开始是非常简单的:
1 | sentence = 'Hello World'
3 | include = False
4 | new_sentence = ''
5 | for char in sentence:
...... char = 'H'
前几行只是向你展示计算机运行的程序行。 左边是编辑器中看到的行号。
运行 for char in sentence: 会给变量 char 赋值,所以 snoop 会显示这个值。 以 ...... 开头的行表示一个新变量或现有变量值的变化。 当这些行是多余的时,它们不会被显示,这就是为什么 snoop 的输出不会像这样开始:
1 | sentence = 'Hello World'
...... sentence = 'Hello World'
3 | include = False
...... include = False
4 | new_sentence = ''
...... new_sentence = ''
5 | for char in sentence:
...... char = 'H'
接下来的两行是:
6 | if include:
8 | include = True
这里重要的是没有显示的内容:因为 include 是 False,所以第 7 行 (new_sentence += char) 被跳过。但随后 include 被设置为 True,所以下一次循环的结果就不同了:
5 | for char in sentence:
...... char = 'e'
6 | if include:
7 | new_sentence += char
.............. new_sentence = 'e'
new_sentence += char 第一次运行,变量 new_sentence 得到一个新值。
现在修改程序,做相反的事情:只打印第一个字符,省略其余部分。
ling830 发表于 2026-07-24 16:48
让我们一起看看 snoop 正在向你展示什么。 一开始是非 ...
明白了!现在要修改程序,让它**只保留第一个字符,省略其余所有字符**。可以用一个布尔标志实现:初始时允许添加,添加第一个字符后立即关闭标志,后续循环就不再添加。
修改后的代码如下:
sentence = 'Hello World'
include = True
new_sentence = ''
for char in sentence:
if include:
new_sentence += char
include = False
print(new_sentence)
运行后输出 H(仅第一个字符)。原理:
- 第1次循环:include为True,执行添加字符'H',然后设置include=False。
- 后续每个字符:include为False,跳过添加,因此只保留了第一个字符。
你也可以试着自己用snoop运行这段修改后的代码,观察include的变化过程,加深理解。
——FishC计算机教育专家
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 if 和 else
condition = True
if condition:
print('Yes')
else:
print('No')
现在将第一行改为 condition = False 并再次运行它。
将 else 理解为“否则”或“否则的话”。这意味着如果 if 中的条件 为假,那么 else 的代码块将会执行。无论条件是真还是假, 两个代码块中恰好有一个会执行。
这是一个更有趣的例子可以运行:
sentence = 'Hello World'
excited = True
if excited:
sentence = sentence.upper()
else:
sentence = sentence.lower()
print(sentence) ling830 发表于 2026-07-24 16:51
if 和 else
condition = True
if condition:
...
if 语句可以搭配 else 使用,让程序在条件不满足时执行另一段代码。
基本结构:
if 条件:
条件为真时执行的代码
else:
条件为假时执行的代码
两个代码块中恰好只有一个会执行。将 else 理解为“否则”。
测试示例:
condition = True
if condition:
print('Yes')
else:
print('No')
将第一行改为 condition = False 后运行,输出变为 No。
更实用的例子——根据布尔值改变字符串大小写:
sentence = 'Hello World'
excited = True
if excited:
sentence = sentence.upper()
else:
sentence = sentence.lower()
print(sentence)
当 excited = True 时,输出 HELLO WORLD;否则输出 hello world。
记住:if 和 else 后面的冒号 : 以及缩进是必须的,缩进区分哪个代码块属于哪个分支。
——FishC计算机教育专家
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 sentence.upper() 是一种我们还没有遇到的新表达式。这里发生的事情是 sentence 是一个字符串,字符串有各种 方法 让你方便地计算出新的值,包括 upper 和 lower。这些名称指的是大写字母(大写字母)和小写字母(小写字母)。'Hello World'.upper() 的结果是 'HELLO WORLD'。不过,它不会改变 sentence 的内容,因此你必须再次用 sentence = sentence.upper() 赋值。
现在将 excited 改为 False 并再次运行它。
这是一个有问题的程序:
sentence = 'Hello World'
excited = True
if excited:
char = '!'
sentence += char
print(sentence)
你能看到问题吗?如果你运行它,一切似乎都很好。可能出什么问题呢?
ling830 发表于 2026-07-24 16:52
sentence.upper() 是一种我们还没有遇到的新表达式。 ...
问题在于当 `excited` 为 `False` 时,变量 `char` 从未被定义,导致 `sentence += char` 这一行会抛出 `NameError`,程序崩溃。
- 当前代码中 `char = '!'` 只在 `if excited:` 的代码块内执行。
- 如果 `excited = False`,则不会执行 `char = '!'`,但后续 `sentence += char` 仍会尝试访问 `char`,由于未定义,Python 会报错。
稳妥的做法是将 `char` 的默认值在 `if` 之前或 `else` 分支中设置,例如:
sentence = 'Hello World'
excited = True
char = '' # 先设定默认值
if excited:
char = '!'
sentence += char
print(sentence)
或者使用 `else` 分支:
if excited:
char = '!'
else:
char = ''
sentence += char
这样无论 `excited` 是 `True` 还是 `False`,`char` 都有定义,程序就能安全运行。
——FishC计算机教育专家
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 sentence = 'Hello World'
excited = False
if excited:
char = '!'
sentence += char
print(sentence)
错误追踪:
6 sentence += char
excited
False
sentence
'Hello World'
NameError: name 'char' is not defined ling830 发表于 2026-07-24 16:54
sentence = 'Hello World'
excited = False
if ex ...
这个错误正是你预期的结果:当 `excited = False` 时,`if` 代码块被跳过,变量 `char` 从未被创建,所以 `sentence += char` 尝试使用一个不存在的名称,抛出 NameError。
**问题根源**:变量 `char` 只在 `if` 分支中定义,但程序在 `if` 之后直接引用了它,没有考虑 `if` 不成立的情况。
**解决方法**:
确保变量在任何路径下都被定义。可以在 `if` 之前给 `char` 一个默认值,或者使用 `else` 分支:
方案1(默认空字符串):
sentence = 'Hello World'
excited = False
char = ''
if excited:
char = '!'
sentence += char
print(sentence)
方案2(else分支):
sentence = 'Hello World'
excited = False
if excited:
char = '!'
else:
char = ''
sentence += char
print(sentence)
这样无论 `excited` 是 True 还是 False,`char` 都有值,程序就不会崩溃了。
——FishC计算机教育专家
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 如果 excited 为真,那么 char 被定义,一切运行正常。但如果不是, char 从未被赋值,因此在 sentence += char 中使用它会失败。
通过向 if 添加一个 else 子句来修复这个问题,这样如果 excited 为假,句子的末尾就会添加一个句号(.)而不是感叹号(!)。 ling830 发表于 2026-07-24 16:55
如果 excited 为真,那么 char 被定义,一切运行正常 ...
修复方案:添加 `else` 分支,使 `excited` 为 `False` 时,`char` 赋值为句号。这样变量 `char` 在所有情况下都有定义,程序不会报错。
修改后的代码:
sentence = 'Hello World'
excited = False
if excited:
char = '!'
else:
char = '.'
sentence += char
print(sentence)
现在运行结果:当 `excited = True` 时输出 `Hello World!`,当 `excited = False` 时输出 `Hello World.`。缩进和冒号是关键,不要遗漏。
——FishC计算机教育专家
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]
2