<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>控制流 on 老张开工了</title>
    <link>/%E6%8E%A7%E5%88%B6%E6%B5%81/</link>
    <description>Recent content in 控制流 on 老张开工了</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Thu, 07 May 2026 00:00:00 +0000</lastBuildDate>
    <atom:link href="/%E6%8E%A7%E5%88%B6%E6%B5%81/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>条件判断与分支控制</title>
      <link>/posts/2026/05/conditionals/</link>
      <pubDate>Thu, 07 May 2026 00:00:00 +0000</pubDate>
      <guid>/posts/2026/05/conditionals/</guid>
      <description>到目前为止，我们学习的程序都是按顺序从上到下逐行执行的。但在实际编程中，我们需要程序能够根据不同的条件做出不同的决策——这就要用到条件判断。条件判断是程序控制流的三大结构之一（顺序、分支、循环）。&#xA;if/elif/else 基本结构 Python 使用 if、elif、else 关键字来实现分支控制，最特别的是用缩进而不是花括号来划定代码块。&#xA;基本语法 # if 语句的基本结构 age = 18 if age &amp;gt;= 18: print(&amp;#34;你已成年&amp;#34;) print(&amp;#34;可以考驾照了&amp;#34;) print(&amp;#34;程序结束&amp;#34;) # 输出： # 你已成年 # 可以考驾照了 # 程序结束 if/else 二选一 # if/else 结构 age = 16 if age &amp;gt;= 18: print(&amp;#34;允许进入&amp;#34;) else: print(&amp;#34;禁止进入&amp;#34;) # 输出：禁止进入 if/elif/else 多选一 # 多条件分支 score = 85 if score &amp;gt;= 90: grade = &amp;#34;优秀&amp;#34; elif score &amp;gt;= 80: grade = &amp;#34;良好&amp;#34; elif score &amp;gt;= 70: grade = &amp;#34;中等&amp;#34; elif score &amp;gt;= 60: grade = &amp;#34;及格&amp;#34; else: grade = &amp;#34;不及格&amp;#34; print(f&amp;#34;成绩等级: {grade}&amp;#34;) # 成绩等级: 良好 # 注意：只会执行第一个满足条件的分支 score = 95 if score &amp;gt;= 90: print(&amp;#34;优秀&amp;#34;) # 只执行这个 elif score &amp;gt;= 80: print(&amp;#34;良好&amp;#34;) # 不会执行 elif score &amp;gt;= 60: print(&amp;#34;及格&amp;#34;) # 不会执行 Python 条件判断的特点 # 1.</description>
    </item>
  </channel>
</rss>
