📄 ch06s02.html#
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css"><!--@import url(stylesheet/text.css);@import url(stylesheet/box.css);--></style><title>简明 Python 教程 / 控制流 / if语句 </title></head><body><table width="100%"><tr><th colspan="3" align="center"><span class="header">简明 Python 教程</span></th></tr><th colspan="3" align="center">第6章 控制流</th><tr><th width="20%" align="left"><a href="ch06.html#s01">上一页</a></th><th width="60%" align="center"><span class="header2">if语句</span></th><th align="right"><a href="ch06s03.html">下一页</a></th></tr></table><hr noshade><h1>if语句</h1><p><code>if</code>语句用来检验一个条件, <dfn>如果</dfn> 条件为真,我们运行一块语句(称为 <dfn>if-块</dfn> ), <dfn>否则</dfn> 我们处理另外一块语句(称为 <dfn>else-块</dfn> )。 <dfn>else</dfn> 从句是可选的。</p><h2><a name="using">使用if语句</a></h2><p class="exampletitle"><a name="e61">例6.1 使用if语句</a></p><p class="filebox"><code class="comment">#!/usr/bin/python<br># Filename: if.py</code><br><br><code>number = </code><code class="cite">23</code><br><code>guess = </code><code class="func">int</code><code>(</code><code class="func">raw_input</code><code>(</code><code class="cite">'Enter an integer : '</code><code>))</code><br><br><code class="key">if </code><code>guess == number:</code><br><code class="key"> print </code><code class="cite">'Congratulations, you guessed it.' </code><code class="comment"># New block starts here</code><br><code class="key"> print </code><code class="cite">"(but you do not win any prizes!)" </code><code class="comment"># New block ends here</code><br><code class="key">elif </code><code>guess < number:</code><br><code class="key"> print </code><code class="cite">'No, it is a little higher than that' </code><code class="comment"># Another block</code><br><code class="comment"> # You can do whatever you want in a block ...</code><br><code class="key">else</code><code>:</code><br><code class="key"> print </code><code class="cite">'No, it is a little lower than that' </code><br><code class="comment"> # you must have guess > number to reach here</code><br><br><code class="key">print </code><code class="cite">'Done'</code><br><code class="comment"># This last statement is always executed, after the if statement is executed</code></p><p>(源文件:<a href="code/if.py">code/if.py</a>)</p><h2>输出</h2><p class="codebox"><code>$ python if.py<br>Enter an integer : 50<br>No, it is a little lower than that<br>Done<br>$ python if.py<br>Enter an integer : 22<br>No, it is a little higher than that<br>Done<br>$ python if.py<br>Enter an integer : 23<br>Congratulations, you guessed it.<br>(but you do not win any prizes!)<br>Done</code></p><h2><a name="how">它如何工作</a></h2><p>在这个程序中,我们从用户处得到猜测的数,然后检验这个数是否是我们手中的那个。我们把变量<code>number</code>设置为我们想要的任何整数,在这个例子中是<code>23</code>。然后,我们使用<code>raw_input()</code>函数取得用户猜测的数字。函数只是重用的程序段。我们将在<a href="ch06.html">下一章</a>学习更多关于函数的知识。</p><p>我们为内建的<code>raw_input</code>函数提供一个字符串,这个字符串被打印在屏幕上,然后等待用户的输入。一旦我们输入一些东西,然后按<strong>回车</strong>键之后,函数返回输入。对于<code>raw_input</code>函数来说是一个字符串。我们通过<code>int</code>把这个字符串转换为整数,并把它存储在变量<code>guess</code>中。事实上,<code>int</code>是一个类,不过你想在对它所需了解的只是它把一个字符串转换为一个整数(假设这个字符串含有一个有效的整数文本信息)。</p><p>接下来,我们将用户的猜测与我们选择的数做比较。如果他们相等,我们打印一个成功的消息。注意我们使用了缩进层次来告诉Python每个语句分别属于哪一个块。这就是为什么缩进在Python如此重要的原因。我希望你能够坚持“每个缩进层一个制表符”的规则。你是这样的吗?</p><p>注意<code>if</code>语句在结尾处包含一个冒号——我们通过它告诉Python下面跟着一个语句块。</p><p>然后,我们检验猜测是否小于我们的数,如果是这样的,我们告诉用户它的猜测大了一点。我们在这里使用的是<code>elif</code>从句,它事实上把两个相关联的<code>if else-if else</code>语句合并为一个<code>if-elif-else</code>语句。这使得程序更加简单,并且减少了所需的缩进数量。</p><p><code>elif</code>和<code>else</code>从句都必须在逻辑行结尾处有一个冒号,下面跟着一个相应的语句块(当然还包括正确的缩进)。</p><p>你也可以在一个if块中使用另外一个<code>if</code>语句,等等——这被称为嵌套的<code>if</code>语句。</p><p>记住,<code>elif</code>和<code>else</code>部分是可选的。一个最简单的有效<code>if</code>语句是:</p><p class="filebox"><code class="key">if </code><code class="func">True</code><code>:</code><br><code class="key"> print </code><code class="cite">'Yes, it is true'</code></p><p>在Python执行完一个完整的<code>if</code>语句以及与它相关联的<code>elif</code>和<code>else</code>从句之后,它移向<code>if</code>语句块的下一个语句。在这个例子中,这个语句块是主块。程序从主块开始执行,而下一个语句是<code>print 'Done'</code>语句。在这之后,Python看到程序的结尾,简单的结束运行。</p><p>尽管这是一个非常简单的程序,但是我已经在这个简单的程序中指出了许多你应该注意的地方。所有这些都是十分直接了当的(对于那些拥有C/C++背景的用户来说是尤为简单的)。它们在开始时会引起你的注意,但是以后你会对它们感到熟悉、“自然”。</p><p class="notebox"><span class="boxtitle">给C/C++程序员的注释</span><br>在Python中没有<code>switch</code>语句。你可以使用<code>if..elif..else</code>语句来完成同样的工作(在某些场合,使用<a href="ch09s04.html">字典</a>会更加快捷。)</p><hr noshade><table width="100%"><tr><th width="20%" align="left"><a href="ch06.html#s01">上一页</a></th><th width="60%" align="center"><a href="ch06.html">上一级</a></th><th width="20%" align="right"><a href="ch06s03.html">下一页</a></th></tr><tr><th width="20%" align="left">简介</th><th width="60%" align="center"><a href="index.html">首页</a></th><th align="right">while语句</th></tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -