{"id":9310,"date":"2016-06-07T23:30:25","date_gmt":"2016-06-08T03:30:25","guid":{"rendered":"http:\/\/cscircles.cemc.uwaterloo.ca\/?page_id=9310"},"modified":"2017-06-29T14:20:32","modified_gmt":"2017-06-29T18:20:32","slug":"zh-9","status":"publish","type":"page","link":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-9\/","title":{"rendered":"9: \u5176\u4ed6\uff0c\u548c\uff0c\u6216\uff0c\u975e"},"content":{"rendered":"<!-- Please retain this notice and add more notes if you create a new version.<br \/>\nOriginal lesson author: David Pritchard, daveagp@gmail.com, http:\/\/cscircles.ca<br \/>\nLicense: http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/-->\n<p>\u8fd9\u8282\u8bfe\u4f1a\u8ba9\u4f60\u4ee5\u66f4\u4f18\u96c5\u7684\u65b9\u5f0f\u505a\u4e00\u4e9b\u590d\u6742\u7684\u6848\u4f8b\u68c0\u67e5\uff0c\u901a\u8fc7\u4f7f\u7528\u4e24\u4e2a\u65b0\u7684Python\u8bed\u8a00\u3002<\/p>\n<ul>\n<li>\u5728<code>if \u00abC\u00bb<\/code>\u4ece\u53e5\u4e4b\u540e\uff0c\u5982\u679c<code>C<\/code><em>\u9519\u8bef<\/em>\uff0c\u4f1a\u8fd0\u884c<code>else<\/code>\u4ece\u53e5\u3002<\/li>\n<li>\u7ed3\u5408\u5e03\u5c14\u6761\u4ef6\uff0c\u901a\u8fc7\u4f7f\u7528<code>A and B<\/code>\uff0c<code>A or B<\/code>\u548c<code>not A<\/code><\/li>\n<\/ul>\n<h1><code>else<\/code><\/h1>\n<p>\u7f16\u5199\u7a0b\u5e8f\u65f6\u4e00\u4e2a\u666e\u904d\u7684\u4efb\u52a1\u662f\u4f60\u60f3\u8981\u68c0\u9a8c\u4e00\u4e9b\u6761\u4ef6\uff0c\u6839\u636e\u6761\u4ef6\u6b63\u786e\u4e0e\u5426\uff0c\u6267\u884c\u4e00\u4e2a\u6216\u53e6\u4e00\u4e2a\u52a8\u4f5c\u3002<a href=\"\/6-if\/\">\u4e4b\u524d<\/a>\uff0c\u6709\u4e00\u4e2a\u7ec3\u4e60\u8981\u4f60\u505a\u4e00\u4e9b\u4e8b\u60c5\u5982\u679c\u5e74\u9f84<code>age<\/code>\u5c0f\u4e8e0\uff0c\u505a\u53e6\u4e00\u4e9b\u4e8b\u60c5\u5982\u679c\u5e74\u9f84<code>age<\/code>\u5927\u4e8e\u7b49\u4e8e0\uff1b\u4e0b\u9762\u662f\u4e00\u4e2a\u53c2\u8003\u7b54\u6848<\/p>\n<pre>if age &gt;= 0: \u00a0\r\n  print('You are not necessarily a time traveller')\r\nif age &lt; 0:\r\n \u00a0print('You are a time traveller')<\/pre>\u73b0\u5728\u8ba9\u6211\u4eec\u4f7f\u7528<code>else<\/code>\u91cd\u65b0\u7f16\u5199\u4e00\u4e0b\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform0\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox0'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u00a0<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 136px;'><textarea wrap='off' name='usercode0' id='usercode0'  cols=10 rows=5 readonly='readonly'  style = 'height : 136px;'  class='pyboxCode RO'>\nage = int(input())\nif age &gt;= 0:\n  print('You are not necessarily a time traveller')\nelse:    # instead of a second 'if'!\n  print('You are a time traveller')<\/textarea><\/div>\n<div id='pbhistory0' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput0\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit0' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch0\" value=\"Input Switch\" onclick=\"pbInputSwitch(0,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(0)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(0,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse0\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"0\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"4fff70335a8bea800b809414452d8a8c\"\/>\n<div id='pbresults0' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>pbInputSwitch(0,\"N\");<\/script>\n<\/p>\n<p>\u901a\u5e38\u6765\u8bf4\uff0c\u4f60\u901a\u8fc7\u5982\u4e0b\u7684\u8bed\u53e5\u6765\u4f7f\u7528<code>else<\/code>\uff1a<\/p>\n<pre>if \u00abtest\u00bb:\r\n   \u00abtrue-body\u00bb        # an indented block\r\nelse:\r\n   \u00abfalse-body\u00bb       # another indented block<\/pre>Python\u4f1a\u8fdb\u884c\u6d4b\u8bd5\u3002\u5982\u679c\u6d4b\u8bd5\u901a\u8fc7\uff0c\u90a3\u4e48\u901a\u8fc7\u4e4b\u540e\u505a\u4e00\u4e9b\u4e8b\u60c5\uff0c\u5982\u679c\u6d4b\u8bd5\u6ca1\u901a\u8fc7\uff0c\u90a3\u4e48\u505a\u53e6\u4e00\u4e9b\u4e8b\u60c5\u3002<\/p>\n<h2><code>else<\/code>\u7684\u54f2\u5b66<\/h2>\n<p>\u6211\u4eec\u4f7f\u7528<code>else<\/code>\u6ca1\u6709\u65b0\u7684\u529f\u80fd\uff0c\u4f46\u662f\u5b83\u786e\u5b9e\u8ba9\u6211\u4eec\u7684\u4ee3\u7801\u66f4\u4fbf\u4e8e\u9605\u8bfb\uff0c\u8c03\u8bd5\uff0c\u7ef4\u62a4\u3002\u4e0b\u9762\u7684\u4e24\u6bb5\u4ee3\u7801\u505a\u4e86\u540c\u4e00\u4ef6\u4e8b\uff1a<\/p>\n<table id=\"AB\">\n<tbody>\n<tr>\n<td>\n<div>\u7248\u672cA<\/div>\n<pre>if height &lt; 256:\r\n   print('Too short for this ride')\r\nelse:\r\n   print('Welcome aboard!')<\/pre>\n<\/td>\n<td>\n<div>\u7248\u672cB<\/div>\n<pre>if height &lt; 256:\r\n   print('Too short for this ride')\r\nif height &gt;= 256:\r\n   print('Welcome aboard!')<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e24\u4e2a\u90fd\u505a\u4e86\u540c\u4e00\u4ef6\u4e8b\uff0c<strong>B<\/strong>\u751a\u81f3\u4e0d\u9700\u8981<code>else<\/code>\u3002\u7136\u800c\uff0c\u5927\u90e8\u5206\u7f16\u7a0b\u4eba\u5458\u4f1a\u89c9\u5f97<strong>A<\/strong>\u66f4\u597d\u3002\u6bd4\u5982\uff0c\u5bf9\u4e8e<strong>A<\/strong>\u6765\u8bf4\uff0c\u6539\u53d8\u6761\u4ef6\uff08\u6bd4\u5982\u4f7f\u7528128\u800c\u4e0d\u662f256\uff09\u4ec5\u9700\u8981\u4e00\u5904\u6539\u52a8\u800c\u4e0d\u662f\u4e24\u5904\u3002\u540c\u6837\uff0c<strong>A<\/strong>\u53ef\u4ee5\u7acb\u523b\u88ab\u4eba\u8bfb\u61c2\uff0c\u800c\u5728<strong>B<\/strong>\u4e2d\u4f60\u9700\u8981\u68c0\u67e5\u4e24\u4e2a\u6761\u4ef6\u901a\u8fc7\u4e0e\u5426\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform1\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox1'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>\u7edd\u5bf9\u503c<\/span><\/div>\u4e00\u4e2a\u6570\u7684<em>\u7edd\u5bf9\u503c<\/em>\u662f\u6839\u636e\u4ee5\u4e0b\u5b9a\u4e49\u7684\u3002\u5bf9\u4e8e\u4e00\u4e2a\u5927\u4e8e\u7b49\u4e8e0\u7684\u6570<em>x<\/em>\uff0c<em>x <\/em>\u7684\u7edd\u5bf9\u503c\u662f<em>x<\/em>\u3002\u53cd\u4e4b\uff0c\u5f53<em>x<\/em>\u5c0f\u4e8e0\uff0c<em>x<\/em>\u7684\u7edd\u5bf9\u503c\u662f<em>-x<\/em>\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c\u548c<em>x<\/em>\u4e00\u6837\u4f46\u662f\u6ca1\u6709\u8d1f\u53f7\u3002\u6bd4\u5982\uff0c<code>5<\/code>\u7684\u7edd\u5bf9\u503c\u662f<code>5<\/code>\uff0c<code>-10<\/code>\u7684\u7edd\u5bf9\u503c\u662f<code>10<\/code>\u3002\u901a\u8fc7\u4f7f\u7528<code>if<\/code>\u548c<code>else<\/code>\uff0c\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u9700\u8981\u4e00\u4e2a\u6574\u6570\u4f5c\u4e3a\u8f93\u5165\uff0c\u6253\u5370\u51fa\u5b83\u7684\u7edd\u5bf9\u503c\u3002<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode1' id='usercode1'  cols=10 rows=20   class='pyboxCode RW'>\nx=int(input())\n<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode1' value='x=int(input())\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit1' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(1)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(1,'N')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(1,'9.abs')\" ><\/td>\n<\/tr><\/table><select id='pbSelect1' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='default' data-pbonclick=\"pbSetText(1,descape($('#defaultCode1').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(1);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse1\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"1\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"b6a19cf514eb9dd48d09c9048f48f191\"\/>\n<div id='pbresults1' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(1);});document.getElementById(\"submit1\").value = \"Run program\";document.getElementById(\"inputInUse1\").value = \"N\";<\/script>\n<\/p>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> Python\u5185\u5efa\u65b9\u7a0b<code>abs(x)<\/code>\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97<code>x<\/code>\u7684\u7edd\u5bf9\u503c\u3002\u4e0a\u9762\u7684\u9898\u76ee\u4e2d\u4e0d\u5141\u8bb8\u4f60\u4f7f\u7528\uff0c\u4f46\u662f\u5728\u4ee5\u540e\u7684\u7a0b\u5e8f\u4e2d\u4f60\u53ef\u4ee5\u4f7f\u7528\u3002 <\/span><\/td><\/table><\/p>\n<h2><code>elif<\/code><\/h2>\n<p>Python\u8fd8\u6709\u53e6\u4e00\u4e2a\u5173\u952e\u8bcd<code>elif<\/code>\uff0c\u53ef\u4ee5\u8ba9\u4e00\u884c\u4e2d\u7684\u4e0d\u540c\u6761\u4ef6\u67e5\u770b\u8d77\u6765\u66f4\u7b80\u5355\u3002\u6700\u57fa\u672c\u7684\u4f7f\u7528<code>elif<\/code>\u7684\u6a21\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre>if \u00abtest1\u00bb:\r\n   \u00abbody1\u00bb        # \u5982\u679ctest1\u6b63\u786e\u5219\u8fd0\u884c\r\nelif \u00abtest2\u00bb:\r\n   \u00abbody2\u00bb        #  \u5982\u679ctest1\u9519\u8bef\uff0ctest2\u6b63\u786e\uff0c\u5219\u8fd0\u884c<\/pre>\u6b63\u5982\u4f60\u6240\u60f3\u7684\u4e00\u6837\uff0c<code>elif<\/code>\u4ee3\u8868\"else if\"\uff0c\u56e0\u4e3a\u5b83\u7b49\u540c\u4e8e\u5728else\u91cc\u9762\u52a0\u4e00\u4e2aif\u3002\u4f46\u662f\uff0c\u4f7f\u7528 elif \u5c31\u4f1a\u7701\u4e0b\u5f88\u591a \u4ee3\u7801\uff0c\u540c\u65f6\u4e5f\u4f1a\u7701\u4e0b\u5f88\u591a\u7f29\u8fdb\uff0c\u8ba9\u4f60\u7684\u7a0b\u5e8f\u66f4\u5bb9\u6613\u9605\u8bfb\uff0c\u8c03\u8bd5\uff0c\u7f16\u8f91\u3002\u6b64\u5916\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<em>\u65e0\u6570\u6b21<\/em>elif\u8bed\u53e5\u5728\u540c\u4e00\u884c\uff0c\u751a\u81f3\u6216\u5728\u6700\u540e\u52a0\u4e0a\u4e00\u4e2a<code>else<\/code>\u8bed\u53e5\uff1a<\/p>\n<pre>if \u00abtest1\u00bb:\r\n   \u00abbody1\u00bb      # \u5982\u679ctest1\u6b63\u786e\u5219\u8fd0\u884c\r\nelif \u00abtest2\u00bb:\r\n   \u00abbody2\u00bb      # \u5982\u679ctest1\u9519\u8bef\uff0ctest2\u6b63\u786e\uff0c\u5219\u8fd0\u884c\r\nelif \u00abtest3\u00bb:\r\n   \u00abbody3\u00bb      # \u5982\u679ctest1\u548ctest2\u4e3a\u9519\u8bef\uff0ctest3\u4e3a\u6b63\u786e\uff0c\u5219\u8fd0\u884c\r\nelse:           # \u6700\u540e\u8fd9\u4e24\u884c\u662f\u53ef\u9009\u7684\r\n   \u00abelse-body\u00bb  # \u5982\u679c\u6240\u6709tests\u90fd\u4e3a\u9519\u8bef\uff0c\u5219\u8fd0\u884c<\/pre>&nbsp;<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5728\u5faa\u73af\u4e2d\u4f7f\u7528 elif \u7684\u4f8b\u5b50\u3002\u4f60\u80fd\u731c\u5230\u7ed3\u679c\u662f\u4ec0\u4e48 \u5417\uff1f<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=for+x+in+range%281%2C+10%29%3A%0A++if+x+%25+2+%3D%3D+0%3A%0A++++print%28x%2C+%27is+even%27%29%0A++elif+x+%25+3+%3D%3D+0%3A%0A++++print%28x%2C+%27is+an+odd+multiple+of+3%27%29%0A++else%3A%0A++++print%28x%2C+%27not+divisible+by+2+or+3%27%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=450px&rightStdout=1'><\/iframe><\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox2'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>\u7b2c\u4e00\u3001\u7b2c\u4e8c\u3001\u7b2c\u4e09<\/span><\/div>1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th\u53eb\u505a<em>\u5e8f\u6570\u8bcd<\/em>\u3002\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u9700\u8981\u8bfb\u53d6\u4e00\u4e2a1\uff5e9\u4e4b\u95f4\u7684\u6574\u6570<em>x<\/em>\u4f5c\u4e3a\u8f93\u5165\u503c\uff0c\u5e76\u6839\u636e<em>x<\/em>\u8f93\u51fa\u76f8\u5e94\u7684\u5e8f\u6570\u8bcd\u3002<br\/> <strong>\u63d0\u793a<\/strong>\uff1a\u4f60\u4e0d\u9700\u8981\u8bbe\u7acb9\u4e2a\u4e0d\u540c\u7684\u60c5\u51b5\uff1b4\u4e2a\u5c31\u591f\u3002<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode2' id='usercode2'  cols=10 rows=20   class='pyboxCode RW'>\n<\/textarea><\/div>\n<div id='pbhistory2' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit2' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(2)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(2,'N')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(2,'9.ordinal')\" ><\/td>\n<\/tr><\/table><select id='pbSelect2' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='help' data-pbonclick=\"helpClick(2);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse2\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"2\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"3c87887a281e92b8d58740b0be7e9d1c\"\/>\n<div id='pbresults2' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(2);});document.getElementById(\"submit2\").value = \"Run program\";document.getElementById(\"inputInUse2\").value = \"N\";<\/script>\n<\/p>\n<h2>\u5e03\u5c14\u8fd0\u7b97\u7b26\uff1a<code>and<\/code>, <code>or<\/code>, <code>not<\/code><\/h2>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\"<code>and<\/code>\"\uff0c\"<code>or<\/code>\"\u548c\"<code>not<\/code>\"\u7ed3\u5408\u5e03\u5c14\u8fd0\u7b97\u7b26\uff0c\u903b\u8f91\u548c\u82f1\u6587\u4e00\u81f4\u3002<\/p>\n<ul>\n<li><code>A and B<\/code>\u662f\u6b63\u786e\u7684\uff0c\u5982\u679c<code>A<\/code>\u6b63\u786e\uff0c<code>B<\/code>\u4e5f\u6b63\u786e\uff1b\u662f\u9519\u8bef\u7684\u5982\u679c\u4efb\u4f55\u4e00\u4e2a\u662f\u9519\u7684\uff08\u6bd4\u5982\uff0c\u4f60\u4f1a\u6dcb\u6e7f\uff0c\u5982\u679c\u4e0b\u96e8\u4e86<code>and<\/code>\u4f60\u5fd8\u5e26\u4f1e\u4e86\uff09\u3002<\/li>\n<li><code>A or B<\/code>\u662f\u6b63\u786e\u7684\uff0c\u5982\u679c<code>A<\/code>\u6b63\u786e\u6216<code>B<\/code>\u6b63\u786e\uff1b\u662f\u9519\u8bef\u7684\uff0c\u5982\u679c\u4e24\u4e2a\u90fd\u662f\u9519\u8bef\u7684\uff08\u6bd4\u5982\uff0c\u5b66\u6821\u662f\u5173\u7740\u7684\uff0c\u5982\u679c\u662f\u8282\u5047\u65e5<code>or<\/code>\u662f\u5468\u672b\uff09\u3002<\/li>\n<li><code>not A<\/code>\u662f\u6b63\u786e\u7684\uff0c\u5982\u679c<code>A<\/code>\u662f\u9519\u8bef\u7684\uff1b\u662f\u9519\u8bef\u7684\uff0c\u5982\u679c<code>A<\/code>\u662f\u6b63\u786e\u7684\uff08\u6bd4\u5982\uff0c\u4f60\u89c9\u5f97\u997f\uff0c\u5982\u679c\u4f60<code>not<\/code> \u5403\u5348\u996d\uff09<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u81ea\u52a8\u5c55\u793a\u51fa\u6240\u6709\u53ef\u80fd\u6027\u3002\u5c31\u50cf\u662f'\u4e58\u6cd5\u8868'\u4e00\u6837\uff0c\u8fd9\u4e2a\u8868\u53eb\u505a<em>\uff08\u903b\u8f91\u8fd0\u7b97\uff09\u771f\u503c\u8868<\/em>\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform3\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox3'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u771f\u503c\u8868<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 136px;'><textarea wrap='off' name='usercode3' id='usercode3'  cols=10 rows=5 readonly='readonly'  style = 'height : 136px;'  class='pyboxCode RO'>\nprint(\"  A      B       not A  not B  A and B  A or B\")\nprint(\"----------------------------------------------\")\nfor A in [False, True]:               # \u5bf9\u5e8f\u5217\u4f7f\u7528\"for\"\u5faa\u73af\t\n    for B in [False, True]:           # \u4f1a\u5728lesson 13\u8bb2\u5230\n        print(A, \" \", B, \"   \", not A, \" \", not B, \" \", A and B, \" \", A or B)<\/textarea><\/div>\n<div id='pbhistory3' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit3' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(3)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(3,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse3\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"3\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"da432b60a48f281ef12c07851352ee42\"\/>\n<div id='pbresults3' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit3\").value = \"Run program\";document.getElementById(\"inputInUse3\").value = \"N\";<\/script>\n<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>and<\/code>\u7684\u4f8b\u5b50\uff0c\u8be5\u7a0b\u5e8f\u5c06\u6570\u5b57\u8f6c\u6362\u6210\u4e86\u5b57\u6bcd\uff0c\u8fd8\u505a\u4e86\u4e00\u4e9b\u9a8c\u9519\u3002<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform4\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox4'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u4e00\u4e2a<code>and<\/code>\u7684\u793a\u4f8b\uff1a\u5c06\u6570\u5b57\u8f6c\u6362\u6210\u5b57\u6bcd\uff0c1=A\uff0c2=B...\u3002<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 136px;'><textarea wrap='off' name='usercode4' id='usercode4'  cols=10 rows=5 readonly='readonly'  style = 'height : 136px;'  class='pyboxCode RO'>\nx = int(input())\nif x>=1 and x<=26:\n    print('\u5b57\u6bcd', x, '\u5728\u5b57\u6bcd\u8868\u4e2d:', chr(ord('A')+(x-1)))\nelse:\n    print('\u65e0\u6548\u8f93\u5165:', x)<\/textarea><\/div>\n<div id='pbhistory4' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput4\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit4' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch4\" value=\"Input Switch\" onclick=\"pbInputSwitch(4,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(4)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(4,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse4\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"4\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"e80cfb25072a17936c697887d865c788\"\/>\n<div id='pbresults4' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>pbInputSwitch(4,\"N\");<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform5\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox5'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>26\u4e2a\u5b57\u6bcd<\/span><\/div>\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u505a\u4e0a\u9762\u7684\u4f8b\u5b50\u76f8\u53cd\u7684\u4e8b\u60c5\uff1a\u9700\u8981\u8bfb\u53d6\u4e00\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u8f93\u5165\uff0c\uff0c\u8f93\u51fa\u76f8\u5bf9\u5e94\u7684\u6570\u5b57\uff08\u57281\u548c26\u4e4b\u95f4\uff09\u3002\u4f60\u7684\u7a0b\u5e8f\u5e94\u8be5\u53ea\u63a5\u53d7<em>\u5927\u5199<\/em>\u5b57\u6bcd\u3002\u5bf9\u4e8e\u9a8c\u9519\uff0c\u6253\u5370\u51fa<code>invalid<\/code>\u5982\u679c\u8f93\u5165\u4e0d\u662f\u4e00\u4e2a\u5927\u5199\u5b57\u6bcd\u3002<a class=\"hintlink\"  id=\"hintlink6\">\u63d0\u793a<\/a><a class=\"hintlink\"  id=\"hintlink7\">\u5907\u7528\u5b57\u7b26\u4e32\u6bd4\u8f83\u6cd5<\/a><div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode5' id='usercode5'  cols=10 rows=20   class='pyboxCode RW'>\nletter = input()\n<\/textarea><\/div>\n<div id='pbhistory5' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode5' value='letter = input()\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit5' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(5)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(5,'N')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(5,'9.numberify')\" ><\/td>\n<\/tr><\/table><select id='pbSelect5' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='default' data-pbonclick=\"pbSetText(5,descape($('#defaultCode5').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(5);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse5\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"5\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"8dbdb9ef8c3cb9d7824d689631910f7f\"\/>\n<div id='pbresults5' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(5);});document.getElementById(\"submit5\").value = \"Run program\";document.getElementById(\"inputInUse5\").value = \"N\";<\/script>\n<\/p>\n<div class='pybox modeNeutral' id='pybox8'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Multiple Choice Exercise: <\/span><span class='title'>\u6469\u6839\u5f8b<\/span><\/div><div>\u4e0b\u9762\u54ea\u4e00\u4e2a\u7b49\u540c\u4e8e<code>A or B<\/code>\uff1f<\/div><label>Your choice: <\/label><select id=\"pyselect8\"><option value=\"d\" selected>Select one<\/option><option value=\"w\">not (A and B)<\/option><option value=\"r\">not ((not A) and (not B))<\/option><option value=\"w\">(not A) and (not B)<\/option><\/select><div class='pyboxbuttons'><input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" name=\"slug\" value=\"9.demorgan\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick='pbMultiCheck(8)'\/><\/div><div class=\"pbresults\" id=\"pyMultiResults8\"><\/div><div class=\"epilogue\">\u6b63\u786e! \u8fd9\u91cc\u6709\u4e00\u79cd\u65b9\u5f0f\u5f97\u5230\u7b54\u6848\u3002\u9996\u5148\uff0c<code>not (A or B)<\/code> \u662f\u6b63\u786e\u7684\uff0c\u5f53<code>A<\/code>\u548c<code>B<\/code>\u90fd\u662f\u9519\u7684\u3002\u540c\u6837\uff0c<code>(not A) and (not B)<\/code>\u662f\u6b63\u786e\u7684\uff0c\u5f53<code>not A<\/code> \u548c<code>not B<\/code>\u90fd\u662f\u6b63\u786e\u7684\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c <code>A<\/code>\u548c<code>B<\/code>\u90fd\u662f\u9519\u7684\u3002\u6240\u4ee5\u6211\u4eec\u5f97\u5230\u4ee5\u4e0b\u7b49\u5f0f\uff1a<pre>(not A) and (not B) = not (A or B)<\/pre>\u5c06<code>not<\/code>\u653e\u5728\u4e24\u4fa7\uff0c\u6240\u4ee5\u6211\u4eec\u5f97\u5230<pre>not ((not A) and (not B)) = not (not (A or B))<\/pre>\uff0c\u540c\u65f6<code>not(not X))<\/code>\u6052\u7b49\u4e8e<code>X<\/code>\uff0c\u6240\u4ee5<pre>not ((not A) and (not B)) = not (not (A or B)) = A or B<\/pre>\u3002\u8fd9\u79cd\u6539\u5199\u5e03\u5c14\u8868\u8fbe\u5f0f\u7684\u65b9\u5f0f\u662f<a href='http:\/\/en.wikipedia.org\/wiki\/De_Morgan%27s_laws'>De Morgan&#39;s laws<\/a>\u4e4b\u4e00.\n<\/div><\/div>\n<h3>\u8fd0\u7b97\u987a\u5e8f<\/h3>\n<p><span style=\"color: #444444; line-height: 24px;\">\u5e03\u5c14\u8fd0\u7b97\u7b26\u6709\"\u8fd0\u7b97\u987a\u5e8f\"\uff0c\u5c31\u50cf\u662f\u6570\u5b66\u8fd0\u7b97\u4e5f\u6709\u987a\u5e8f\u4e00\u6837\u3002\u987a\u5e8f\u662f<\/span><\/p>\n<p style=\"text-align: center;\"><strong>NAO<\/strong>: not (\u6700\u5148\u7ea7), and, or (\u6700\u540e\u7ea7)<\/p>\n<p style=\"text-align: left;\">\u6240\u4ee5\u8bf4\u6bd4\u5982\uff0c<\/p>\n<p style=\"text-align: center;\"><code>not x or y and z<\/code> <span style=\"padding-right: 40px; padding-left: 40px;\">\u610f\u5473\u7740<\/span> <code>(not x) or (y and z)<\/code><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5b9e\u4f8b\uff0c\u6211\u4eec\u5c06\u4ee5\u4e00\u4e2a\u7b80\u77ed\u7684\u95ee\u9898\u6765\u7ed3\u675f\u8fd9\u8282\u8bfe\u3002<br \/>\n<div class='pybox modeNeutral' id='pybox9'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Multiple Choice Exercise: <\/span><span class='title'>\u8fd0\u7b97\u987a\u5e8f<\/span><\/div><div><\/p>\n<pre>A or not B and C<\/pre>\u7684\u503c\u662f\u4ec0\u4e48\uff0c\u5982\u679c<br \/>\n(A, B, C) = (False, True, True)\uff1f<a class=\"hintlink\"  id=\"hintlink10\">\u63d0\u793a<\/a><\/div><label>Your choice: <\/label><select id=\"pyselect9\"><option value=\"d\" selected>Select one<\/option><option value=\"w\">True<\/option><option value=\"r\">False<\/option><\/select><div class='pyboxbuttons'><input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" name=\"slug\" value=\"9.multi\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick='pbMultiCheck(9)'\/><\/div><div class=\"pbresults\" id=\"pyMultiResults9\"><\/div><div class=\"epilogue\">\u6b63\u786e! \u8fd0\u7b97\u65b9\u5f0f\u8ba9\u8fd9\u4e2a\u7b49\u4ef7\u4e8e<pre>A or ((not B) and C)<\/pre>\u66ff\u6362\u503c\uff0c\u6211\u4eec\u5f97\u5230<pre>False or ((not True) and True)<\/pre>\uff0c\u7136\u540e\u7b80\u5316\u5f97\u5230<pre>False or ((not True) and True)<br\/>= False or (False and True)<br\/>= False or False<br\/>= False<\/pre>\n<\/div><\/div>\u4e00\u65e6\u4f60\u5b8c\u6210\u4e86\u8fd9\u4e2a\uff0c\u4f60\u5c31\u53ef\u4ee5\u4e0a\u4e0b\u4e00\u8282\u8bfe\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u8282\u8bfe\u4f1a\u8ba9\u4f60\u4ee5\u66f4\u4f18\u96c5\u7684\u65b9\u5f0f\u505a\u4e00\u4e9b\u590d\u6742\u7684\u6848\u4f8b\u68c0\u67e5\uff0c\u901a\u8fc7\u4f7f\u7528\u4e24\u4e2a\u65b0\u7684Python\u8bed\u8a00\u3002 \u5728if \u00abC\u00bb\u4ece\u53e5\u4e4b\u540e\uff0c\u5982\u679cC\u9519\u8bef\uff0c\u4f1a\u8fd0\u884celse\u4ece\u53e5\u3002 \u7ed3\u5408\u5e03\u5c14\u6761\u4ef6\uff0c\u901a\u8fc7\u4f7f\u7528A and B\uff0cA or B\u548cnot A else \u7f16\u5199\u7a0b\u5e8f\u65f6\u4e00\u4e2a\u666e\u904d\u7684\u4efb\u52a1\u662f\u4f60\u60f3\u8981\u68c0\u9a8c\u4e00\u4e9b\u6761\u4ef6\uff0c\u6839\u636e\u6761\u4ef6\u6b63\u786e\u4e0e\u5426\uff0c\u6267\u884c\u4e00\u4e2a\u6216\u53e6\u4e00\u4e2a\u52a8\u4f5c\u3002\u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u7ec3\u4e60\u8981\u4f60\u505a\u4e00\u4e9b\u4e8b\u60c5\u5982\u679c\u5e74\u9f84age\u5c0f\u4e8e0\uff0c\u505a\u53e6\u4e00\u4e9b\u4e8b\u60c5\u5982\u679c\u5e74\u9f84age\u5927\u4e8e\u7b49\u4e8e0\uff1b\u4e0b\u9762\u662f\u4e00\u4e2a\u53c2\u8003\u7b54\u6848 if age &gt;= 0: \u00a0 print('You are not necessarily a time traveller') if age &lt; 0: \u00a0print('You are a time traveller')\u73b0\u5728\u8ba9\u6211\u4eec\u4f7f\u7528else\u91cd\u65b0\u7f16\u5199\u4e00\u4e0b\u3002 Example\u00a0 age = int(input()) if age &gt;= 0: &hellip; <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-9\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-9310","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/comments?post=9310"}],"version-history":[{"count":12,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9310\/revisions"}],"predecessor-version":[{"id":10262,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9310\/revisions\/10262"}],"wp:attachment":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/media?parent=9310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}