{"id":9296,"date":"2016-06-07T23:29:57","date_gmt":"2016-06-08T03:29:57","guid":{"rendered":"http:\/\/cscircles.cemc.uwaterloo.ca\/?page_id=9296"},"modified":"2017-06-28T18:32:54","modified_gmt":"2017-06-28T22:32:54","slug":"zh-10","status":"publish","type":"page","link":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-10\/","title":{"rendered":"10: \u5b9a\u4e49"},"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>\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u5411\u4f60\u5c55\u793a\u7f16\u7a0b\u4e2d\u6700\u91cd\u8981\u7684\u5185\u5bb9\uff1a\u5b9a\u4e49\u51fd\u6570\uff01\u51fd\u6570\u8ba9\u4f60\u7684\u7a0b\u5e8f\u53d8\u5f97\u66f4\u77ed, \u66f4\u6574\u6d01, \u4fbf\u4e8e\u9605\u89c8\u548c\u8c03\u8bd5\uff0c\u63d0\u9ad8\u53ef\u91cd\u590d\u4f7f\u7528\u6027\u3002\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u89c1\u5230\u5176\u4ed6\u7684\u597d\u5904\uff0c\u4f8b\u5982<em>\u9012\u5f52<\/em>\u3002<\/p>\n<h1>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<\/h1>\n<p>\u6211\u4eec\u4f1a\u4ece\u4e00\u4e2a\u4f8b\u5b50\u5f00\u59cb\u3002\u8fd8\u8bb0\u5f97<code>abs<\/code>\u8fd9\u4e2a\u51fd\u6570\u4e48? \u5b83\u8bfb\u53d6\u4e00\u4e2a\u5b9e\u53c2\uff08\u4e00\u4e2a\u6570\u5b57 <em>x<\/em>\uff09\uff0c\u7136\u540e\u8fd4\u8fd8\u5b83\u7684\u7edd\u5bf9\u503c\uff08\u5f53<em>x <\/em>\u2265 0\uff0c\u7b49\u4e8e<em>x<\/em>\uff1b\u5f53 <em>x<\/em> &lt; 0\uff0c\u7b49\u4e8e-<em>x<\/em>\uff09\u3002\u5728Python\u4e2d\uff0c\u5b83\u662f\u8fd9\u6837\u5b9a\u4e49\u8fd9\u4e2a\u51fd\u6570\u7684:<\/p>\n<pre>def abs(x):     # \u5b9a\u4e49\u4e00\u4e2a\u5e26\u6709\u4e00\u4e2a\u5b9e\u53c2\"x\"\u7684\u51fd\u6570\u201cabs\u201d\r\n  if x &gt;= 0:    # \u51fd\u6570\u4ece\u8fd9\u91cc\u5f00\u59cb\r\n    return x\r\n  else:\r\n    return -x   # \u51fd\u6570\u5728\u8fd9\u91cc\u7ed3\u675f<\/pre>(\u70b9\u51fb<a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/console\/?consolecode=def%20abs%28x%29%3A%20%23%E5%87%BD%E6%95%B0%E5%AE%9A%E4%B9%89%0A%20%20if%20x%20%26gt%3B%3D%200%3A%0A%20%20%20%20return%20x%0A%20%20else%3A%20%20%0A%20%20%20%20return%20-x%0A%0A%23%20%E4%B8%BB%E7%A8%8B%E5%BA%8F%E4%BB%8E%E8%BF%99%E9%87%8C%E5%BC%80%E5%A7%8B%0Aprint%28abs%28-5%29%29%0Aprint%28abs%2810%29%29\" target=\"_blank\">\u8fd9\u91cc<\/a>\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e2d\u6d4b\u8bd5\u8fd9\u6bb5\u4ee3\u7801\u3002)<\/p>\n<h2>\u51fd\u6570\u5b9a\u4e49\u7684\u4e24\u4e2a\u90e8\u5206<\/h2>\n<p>\u51fd\u6570\u5b9a\u4e49\u7684\u7b2c\u4e00\u884c\u786e\u5b9a\u4e86\u8fd9\u4e2a\u51fd\u6570\u7684<em>\u540d\u5b57<\/em>\u548c<em>\u5b9e\u53c2<\/em>\u3002\u5b83\u5e94\u8be5\u770b\u4e0a\u53bb\u50cf\u8fd9\u6837\uff1a<\/p>\n<p style=\"text-align: center;\"><code>def \u00ab\u51fd\u6570\u540d\u5b57\u00bb(\u00ab\u5b9e\u53c2\u5217\u00bb):<\/code><\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d, <code>abs<\/code>\u662f\u51fd\u6570\u7684\u540d\u5b57\uff0c\u5e76\u4e14\u5b9e\u53c2\u5217\u662f<code>x<\/code>\u3002\u4e4b\u6240\u4ee5\u79f0\u4e4b\u4e3a<em>\u5b9e\u53c2\u5217<\/em>\u662f\u56e0\u4e3a\u8fd9\u91cc\u53ef\u4ee5\u6709\u591a\u4e2a\u5b9e\u53c2\uff0c\u6bd4\u5982<code>x, y, z<\/code>; \u540c\u6837\uff0c\u6ca1\u6709\u5b9e\u53c2\u4e5f\u662f\u53ef\u884c\u7684, \u4f8b\u5982\u4e00\u4e2a\u7a7a\u7684\u5217\u3002<strong>\u8f93\u5165\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u7684\u5b9e\u53c2\u3002<\/strong><\/p>\n<p>\u6240\u6709\u5728\u51fd\u6570\u5b9a\u4e49\u7b2c\u4e00\u884c\u4e4b\u540e\u7684\u5185\u5bb9\u90fd\u662f<em>\u51fd\u6570\u4f53<\/em>\u3002\u51fd\u6570\u4f53\u662f\u4e00\u5757\u7f29\u8fdb\u7684\u4ee3\u7801\u3002\u6bcf\u5f53\u4e00\u4e2a\u51fd\u6570\u88ab\u8c03\u7528, \u51fd\u6570\u4f53\u4f1a\u5728\u8fd9\u4e9b\u5b9e\u53c2\u6216\u8f93\u5165\u4e2d\u6267\u884c\u4efb\u52a1\u3002\u6700\u7ec8, \u5f53\u6211\u4eec\u5230\u8fbe\u51fd\u6570\u4f53\u4e2d\u7684\u8fd9\u884c,<\/p>\n<p style=\"text-align: center;\"><code>return \u00ab\u503c\u00bb<\/code><\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u5c31\u4f1a\u505c\u6b62\u8fd0\u884c\u5e76\u8fd4\u8fd8<code>\u00ab\u503c\u00bb<\/code>\u4f5c\u4e3a\u5b83\u7684\u8f93\u51fa<strong>\u3002\u6211\u4eec\u53ef\u4ee5\u7406\u89e3\u4e3a, <\/strong><strong>\u8fd4\u8fd8\u7684\u503c\u662f\u8fd9\u4e2a\u51fd\u6570\u7684\u8f93\u51fa\u3002<\/strong>\u00a0\u5728 <code>abs<\/code> \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570\u4f53\u4e2d\u542b\u6709\u591a\u4e2a<code>return<\/code> \u8bed\u53e5\uff1b\u4f46\u53ea\u6709\u7b2c\u4e00\u4e2a\u88ab\u6267\u884c\u7684\u4f1a\u4ea7\u751f\u4f5c\u7528\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5728\u6267\u884c\u7b2c\u4e00\u4e2a\u540e\u505c\u6b62\u3002<\/p>\n<h2>\u793a\u4f8b: \u5b9a\u4e49\u548c\u8c03\u7528\u4e00\u4e2a\u51fd\u6570<\/h2>\n<p>\u4e0b\u9762\u7684\u8fd9\u4e2a\u7a0b\u5e8f\u542b\u6709\u4e00\u4e2a\u51fd\u6570\u5b9a\u4e49\u548c\u4e00\u4e9b\u4f1a\u5728\u8fd9\u4e2a\u51fd\u6570\u88ab\u5b9a\u4e49\u540e\u6267\u884c\u7684\u8bed\u53e5\u3002\u4f60\u80fd\u731c\u5230\u8f93\u51fa\u4f1a\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=def+square%28x%29%3A+++++%23+%E5%87%BD%E6%95%B0%E5%AE%9A%E4%B9%89%0A++++return+x%2Ax+++++%23+%E5%87%BD%E6%95%B0%E4%BD%93%E5%8F%AA%E6%9C%89%E4%B8%80%E8%A1%8C%0A%0A%23+%E5%BD%93%E5%AE%83%E8%A2%AB%E5%AE%9A%E4%B9%89%E5%A5%BD%E5%90%8E%EF%BC%8C%E4%BD%BF%E7%94%A8%E6%AD%A4%E5%87%BD%E6%95%B0%0Aprint%28square%2810%29%29++++++++%0Aprint%28square%28square%282%29%29%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=460&rightStdout=1'><\/iframe><\/p>\n<p>\u6211\u4eec\u770b\u5230\uff0c\u51fd\u6570\u7684\u540d\u5b57\u662f <code>square<\/code>, \u5b83\u53ea\u6709\u4e00\u4e2a\u5b9e\u53c2 <code>x<\/code>, \u5b83\u7684\u51fd\u6570\u4f53\u4e5f\u53ea\u6709\u4e00\u884c\uff1a<code>return x*x<\/code>\u3002\u5728\u51fd\u6570\u7684\u5916\u9762\u6211\u4eec\u6709\u4e24\u4e2a\u6307\u4ee4, \u4ed6\u4eec\u603b\u5171\u8c03\u7528\u4e86\u8fd9\u4e2a\u51fd\u6570\u4e09\u6b21\u3002<\/p>\n<p>\u53ef\u89c6\u5316\u7a97\u53e3\u5411\u6211\u4eec\u5c55\u793a\u4e86\u4e00\u4e2a\u72ec\u7acb\u7684\u5de5\u4f5c\u7a7a\u95f4\uff08\u4e00<strong>\u5e27<\/strong>\uff09\u5728\u6bcf\u4e00\u6b21\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u90fd\u4f1a\u88ab\u5206\u914d\u3002\u5f53\u51fd\u6570\u8fd4\u8fd8\u65f6\uff0c\u8fd9\u4e2a\u5de5\u4f5c\u7a7a\u95f4\u5c31\u4e0d\u518d\u9700\u8981\u5e76\u88ab\u6e05\u9664\u4e86\u3002<\/p>\n<p>\u8fd9\u91cc\u662f\u6bcf\u4e00\u6b65\u7684\u89e3\u91ca:<\/p>\n<ol>\n<li>\u5f53\u7b2c\u4e00\u4e2a\u6307\u4ee4\u88ab\u6267\u884c, Python\u5fc5\u987b\u8fd0\u7b97<code>square(10)<\/code>.\n<ul>\n<li>Python\u4f1a\u5c06\u8f93\u5165\u5217<code>(10)<\/code>\u548c\u5b9e\u53c2\u5217<code>(x)<\/code>\u8fdb\u884c\u5339\u914d\u3002\u6240\u4ee5\uff0c\u5b83\u4f1a\u6267\u884c\u51fd\u6570\u4f53<code>return x*x<\/code>\u3002\u540c\u65f6\u9700\u8981\u8bb0\u4f4f\u7684\u662f <code>x<\/code>\u7b49\u4e8e<code>10<\/code>\u3002\u6240\u4ee5<code>x*x<\/code>\u4f1a\u4ea7\u751f<code>100<\/code>\uff0c\u5e76\u5c06\u5176\u6253\u5370\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5f53\u7b2c\u4e8c\u4e2a\u6307\u4ee4\u88ab\u6267\u884c, Python\u5fc5\u987b\u8fd0\u7b97<code>square(square(2))<\/code>.\n<ul>\n<li>\u5185\u90e8\u90e8\u5206<code>square(2)<\/code>\u662f\u5148\u88ab\u8fd0\u7b97\u7684\u3002\u6211\u4eec\u6682\u65f6\u5148\u8bbe<code>x<\/code>\u7b49\u4e8e<code>2<\/code>\uff0c\u5e76\u8fd0\u884c\u51fd\u6570\u4f53\u3002\u5b83\u4f1a\u8fd4\u8fd8<code>4<\/code>\u3002<\/li>\n<li>\u63a5\u7740\u5916\u90e8\u7684\u8868\u8fbe\u5f0f\u4f1a\u88ab\u8fd0\u7b97; \u56e0\u4e3a <code>square(2)<\/code> \u4f1a\u7ed9\u51fa <code>4<\/code>, \u6211\u4eec\u73b0\u5728\u4f1a\u8c03\u7528 <code>square(4)<\/code>. \u5b83\u4f1a\u8fd4\u8fd8 <code>16<\/code>, \u5e76\u5c06\u5176\u6253\u5370\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>\u56db\u4e2a\u5e38\u89c1\u9519\u8bef<\/h2>\n<p>\u4e00\u4e2a\u5728\u5b9a\u4e49\u51fd\u6570\u4e2d\u7684\u5e38\u89c1\u9519\u8bef\u5c31\u662f\u5fd8\u8bb0<code>return<\/code>\u8bed\u53e5\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>\u9519\u8bef1\uff1a\u5fd8\u8bb0<code>return<\/code><div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode0' id='usercode0'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\ndef square(x):\n  x*x\nprint(square(10))<\/textarea><\/div>\n<div id='pbhistory0' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit0' value=' '\/><\/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=\"d401a99df9d67e64488be6a811464442\"\/>\n<div id='pbresults0' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit0\").value = \"Run program\";document.getElementById(\"inputInUse0\").value = \"N\";<\/script>\n<\/p>\n<p>\u5982\u679c\u51fd\u6570\u4f53\u4e2d\u6ca1\u6709<code>return<\/code>\u8bed\u53e5\uff0c\u90a3\u4e48\u51fd\u6570\u4f1a\u9ed8\u8ba4\u7ed9\u51fa<code>None<\/code>\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u7684\u4e00\u4e9b\u7ec3\u4e60\u56e0\u4e3a\u51fd\u6570\u8fd4\u8fd8<code>None<\/code>\u800c\u6ca1\u6709\u6210\u529f, \u90a3\u4e48\u95ee\u9898\u901a\u5e38\u662f\u67d0\u4e9b\u51fd\u6570\u8f93\u5165\u5e76\u6ca1\u6709\u4f7f<code>return<\/code>\u8bed\u53e5\u88ab\u6267\u884c\u3002<\/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> \u6709\u4e8b\u4e5f\u53ef\u80fd\u4f1a\u7279\u610f\u5ffd\u7565<code>return<\/code>\u8bed\u53e5\u3002\u8fd9\u79cd\u60c5\u51b5\u53ea\u6709\u5f53\u4f60\u7684\u51fd\u6570\u6709\u4e00\u4e9b\u9664\u4e86\u8fd4\u56de\u503c\u5916\u7684\u526f\u4f5c\u7528\u624d\u53ef\u884c\uff0c\u4f8b\u5982\u6253\u5370\u4e00\u4e9b\u8f93\u51fa\uff1a<form class=\"pbform\" action=\"#\" id=\"pbform1\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox1'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>\u6709\u4e00\u4e2a\u526f\u4f5c\u7528\u5e76\u4e14\u6ca1\u6709return\u8bed\u53e5\u7684\u793a\u4f8b<\/span><\/div><div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 136px;'><textarea wrap='off' name='usercode1' id='usercode1'  cols=10 rows=5 readonly='readonly'  style = 'height : 136px;'  class='pyboxCode RO'>\ndef repeatedPrint(string, repetitions):   # \u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\u6ca1\u6709 returns \n  for i in range(0, repetitions):\n    print(string)\n\nrepeatedPrint('Hello, world!', 3)<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\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<\/tr><\/table><\/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=\"3a4e4aab27599b79eca20822f175c9e1\"\/>\n<div id='pbresults1' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit1\").value = \"Run program\";document.getElementById(\"inputInUse1\").value = \"N\";<\/script>\n  <\/span><\/td><\/table><\/p>\n<p>\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u662f\u5fd8\u8bb0\u7f29\u8fdb\u4ee3\u7801, \u4f1a\u9020\u6210<code>IndentationError<\/code>\u9519\u8bef\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox2'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u9519\u8bef2: \u5fd8\u8bb0\u7f29\u8fdb<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode2' id='usercode2'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\ndef square(x):\nreturn x*x\nprint(square(10))<\/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<\/tr><\/table><\/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=\"76a9707ae41952893106ccb9765ef1ba\"\/>\n<div id='pbresults2' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit2\").value = \"Run program\";document.getElementById(\"inputInUse2\").value = \"N\";<\/script>\n<\/p>\n<p>\u5c31\u50cf\u6211\u4eec\u5728\u7b2c2\u8bfe\u4e2d\u770b\u5230\u7684\uff0c\u8c03\u7528\u51fd\u6570\u65f6\u8fc7\u591a\u6216\u8fc7\u5c11\u7684\u5b9e\u53c2\u90fd\u4f1a\u9020\u6210\u9519\u8bef\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>\u9519\u8bef3: \u8c03\u7528\u65f6\u8fc7\u591a\u5b9e\u53c2<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode3' id='usercode3'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\ndef square(x):\n  return x*x\nprint(square(7, 11))<\/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=\"f5b2120c0fc6ad7f25132dea204179c4\"\/>\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>\u6700\u540e\uff0c\u5982\u679c\u4f60\u5728\u5b9a\u4e49\u6216\u8005\u8c03\u7528\u51fd\u6570\u65f6\u6253\u9519\u5b57\uff0c\u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\u9519\u8bef\uff0c\u8bf4\u8fd9\u4e2a\u51fd\u6570\u672a\u5b9a\u4e49\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>\u9519\u8bef4: \u9519\u8bef\u540d\u5b57<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode4' id='usercode4'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\ndef square(x):\n  return x*x\nprint(skware(10))<\/textarea><\/div>\n<div id='pbhistory4' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit4' value=' '\/><\/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=\"5155fcb74e9e404c28b06beff82cd471\"\/>\n<div id='pbresults4' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit4\").value = \"Run program\";document.getElementById(\"inputInUse4\").value = \"N\";<\/script>\n<\/p>\n<h2>\u81ea\u5df1\u5c1d\u8bd5<\/h2>\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'>Cubism<\/span><\/div>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<code>cube(n)<\/code>\uff0c\u628a\u5355\u72ec\u4e00\u4e2a\u6570\u5b57<code>n<\/code>\u00a0\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8f93\u51fa\u5b83\u7684\u7acb\u65b9<code>n<\/code>\u00a0\u00d7\u00a0<code>n<\/code>\u00a0\u00d7\u00a0<code>n<\/code>.<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'>\n<\/textarea><\/div>\n<div id='pbhistory5' class='flexcontain' style='display:none;'><\/div>\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,'Y')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(5,'10.cube')\" ><\/td>\n<\/tr><\/table><select id='pbSelect5' class='selectmore'><option name='more'>More actions...<\/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=\"ccb37b73b45602cbe14598d08d7d10b6\"\/>\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<h2>\u4e24\u4e2a\u6216\u591a\u4e2a\u5b9e\u53c2<img decoding=\"async\" class=\"alignright\" src=\"..\/wp-content\/lesson_files\/img\/10\/area-triangle-zh.png\" alt=\"\" \/><\/h2>\n<p>\u4e0a\u9762\u7684\u8fd9\u4e9b\u51fd\u6570\u90fd\u53ea\u6709\u4e00\u4e2a\u5b9e\u53c2, \u4f46\u5b9e\u9645\u4e0a\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u6709\u4efb\u610f\u6570\u91cf\u7684\u5b9e\u53c2\u3002\u4f8b\u5982\uff0c\u4f60\u66fe\u7ecf\u8c03\u7528\u8fc7\u7684<code>input()<\/code>\u5e76\u4e0d\u9700\u8981\u4efb\u4f55\u5b9e\u53c2\uff08\u4f60\u4f1a\u572815A\u4e2d\u5b9a\u4e49\u6ca1\u6709\u5b9e\u53c2\u7684\u51fd\u6570\uff09\u3002<\/p>\n<p>\u8fd9\u91cc\u662f\u4e00\u4e2a\u6709<strong>\u4e24\u4e2a\u5b9e\u53c2<\/strong>\u7684\u4f8b\u5b50\uff0c\u5b83\u548c\u51e0\u4f55\u6709\u5173\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u4e09\u89d2\u5f62\u7684\u9762\u79ef\uff0c\u5b83\u7684\u5e95\u548c\u9ad8\u7684\u957f\u5ea6\u662f\u5df2\u77e5\u7684\u3002\u8ba1\u7b97\u9762\u79ef\u7684\u516c\u5f0f\u662f<\/p>\n<p style=\"text-align: center;\">\u9762\u79ef = \u5e95 \u00d7 \u9ad8 \/ 2<\/p>\n<p>\u5728\u4ee3\u7801\u4e2d, \u5b83\u770b\u4e0a\u53bb\u5411\u4e0b\u9762\u8fd9\u6837\uff1a\u6211\u4eec\u7528<code>def \u00ab\u51fd\u6570\u540d\u00bb(\u00ab\u5b9e\u53c21\u00bb, \u00ab\u5b9e\u53c22\u00bb)<\/code>\u66ff\u6362<code>def \u00ab\u51fd\u6570\u540d\u00bb(\u00ab\u5b9e\u53c21\u00bb)<\/code>\u3002<\/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=def+triangleArea%28base%2C+height%29%3A%0A++return+base%2Aheight%2F2%0Aprint%28triangleArea%283%2C+5%29%29%0Aprint%28triangleArea%284%2C+2%29%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=345&rightStdout=1'><\/iframe><\/p>\n<p>\u6ce8\u610f\uff0c\u5f53\u4e00\u4e2a\u51fd\u6570\u88ab<em>\u5b9a\u4e49<\/em>\u548c\u88ab<em>\u6267\u884c<\/em>\u662f\u4e0d\u540c\u7684\u3002\u51fd\u6570\u4f53\u53ea\u6709\u5f53\u8fd9\u4e2a\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u624d\u4f1a\u6267\u884c\u3002\u8bf7\u5b8c\u6210\u4e0b\u9762\u8fd9\u4e2a\u8054\u7cfb\u6765\u638c\u63e1\u8fd9\u4e2a\u77e5\u8bc6\u70b9\uff1a<\/p>\n<div class='pybox modeNeutral' id='pybox6'>\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'>In-n-Out<\/span><\/div><div>\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u8f93\u51fa\u662f\u4ec0\u4e48\uff1f<\/p>\n<pre>def f():          # 0\u4e2a\u5b9e\u53c2\u7684\u51fd\u6570\r\n   print(\"in f\")\r\nprint(\"not in f\")\r\nf()               # \u8c03\u7528f<\/pre><\/div><label>Your choice: <\/label><select id=\"pyselect6\"><option value=\"d\" selected>Select one<\/option><option value=\"r\">\u4e24\u884c: 'not in f', \u63a5\u7740 'in f'<\/option><option value=\"w\">\u4e09\u884c: 'in f', \u63a5\u7740 'not in f', \u8ddf\u7740 'in f'<\/option><option value=\"w\">\u4e24\u884c: 'in f', \u63a5\u7740 'not in f'<\/option><\/select><div class='pyboxbuttons'><input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" name=\"slug\" value=\"10.order\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick='pbMultiCheck(6)'\/><\/div><div class=\"pbresults\" id=\"pyMultiResults6\"><\/div><div class=\"epilogue\">\u6b63\u786e! \u51fd\u6570<code>f<\/code>\u662f\u5df2\u7ecf\u88ab\u5b9a\u4e49\u7684\uff0c\u4f46\u5b83\u7684\u51fd\u6570\u4f53\u5e76\u6ca1\u6709\u88ab\u7acb\u5373\u6267\u884c\u3002\u5f53\u8fd9\u4e2a\u51fd\u6570\u88ab\u5b9a\u4e49\u540e\uff0c\u6211\u4eec\u6253\u5370\u51fa<code>not in f<\/code>\u3002\u7136\u540e\u8fd9\u4e2a\u51fd\u6570\u624d\u88ab\u8c03\u7528\uff0c\u5e76\u4e14\u5b83\u7684\u51fd\u6570\u4f53\u88ab\u6267\u884c\uff0c\u6253\u5370 <code>in f<\/code>\u3002<\/div><\/div>\n<p><img decoding=\"async\" class=\"alignright\" style=\"color: #333333; font-style: normal; line-height: 24px; margin-top: 0.4em;\" src=\"..\/wp-content\/lesson_files\/img\/10\/peri-rect-zh.png\" alt=\"\" \/><\/p>\n<p>\u5728\u4e0a\u4e2a\u7ec3\u4e60\u4e2d\uff0c\u6211\u4eec\u8981\u6c42\u4f60\u5199\u4e86\u4e00\u4e2a\u6709\u4e24\u4e2a\u5b9e\u53c2\u7684\u51fd\u6570\uff0c\u6765\u8ba1\u7b97\u957f\u65b9\u5f62\u7684\u5468\u957f(\u5468\u957f\u662f\u6240\u6709\u8fb9\u957f\u5ea6\u7684\u603b\u548c)\u3002\u5f53\u957f\u548c\u5bbd\u5df2\u77e5\u65f6\uff0c\u5468\u957f\u7684\u516c\u5f0f\u662f:<\/p>\n<p style=\"text-align: center;\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Ctextrm%7Bperimeter+%3D+width+%2B+height+%2B+width+%2B+height%7D&#038;bg=T&#038;fg=000000&#038;s=2' alt='\\textrm{perimeter = width + height + width + height}' title='\\textrm{perimeter = width + height + width + height}' class='latex' \/><\/p>\n<p style=\"text-align: left;\">\u8bf7\u53c2\u7167\u53f3\u8fb9\u7684\u4f8b\u56fe\u3002<\/p>\n<p style=\"text-align: left;\"><br\/> <br\/><\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform7\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox7'>\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'>Rectangle<\/span><\/div>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<code>rectanglePerimeter(width, height)<\/code>\uff0c\u6765\u8ba1\u7b97\u957f\u65b9\u5f62\u7684\u5468\u957f\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='usercode7' id='usercode7'  cols=10 rows=20   class='pyboxCode RW'>\n<\/textarea><\/div>\n<div id='pbhistory7' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit7' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(7)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(7,'Y')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(7,'10.rectangle')\" ><\/td>\n<\/tr><\/table><select id='pbSelect7' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='help' data-pbonclick=\"helpClick(7);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse7\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"7\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"5eadeed428c169c724ad88138297e7aa\"\/>\n<div id='pbresults7' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(7);});document.getElementById(\"submit7\").value = \"Run program\";document.getElementById(\"inputInUse7\").value = \"N\";<\/script>\n<\/p>\n<h3>\u51fd\u6570\u8c03\u7528\u51fd\u6570<\/h3>\n<p>\u51fd\u6570\u662f\u5efa\u7acb\u826f\u597d\u7684\u5927\u578b\u7a0b\u5e8f\u7684\u57fa\u77f3\uff1a\u4f60\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u5199\u540c\u6837\u7684\u4ee3\u7801\u6765\u5b8c\u6210\u540c\u6837\u7684\u4efb\u52a1\uff0c\u4f60\u4e5f\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u4f60\u7684\u7b54\u6848\u6765\u5b8c\u6210\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5efa\u7acb\u4e00\u4e2a\u51fd\u6570\u5e76\u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u4e2d\u8c03\u7528\u5b83\u3002<\/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> <em>\u5728\u4f60\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u4e4b\u524d\u4f60\u53ef\u4ee5\u731c\u5230\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\u5417\uff1f<\/em>\u5728\u7b2c4\u8bfe\u4e2d\uff0c\u6211\u4eec\u5b66\u5230\u7528\u5b57\u7b26\u4e32\u4e58\u4ee5\u4e00\u4e2a\u6574\u6570\u610f\u5473\u7740\u91cd\u590d\u8fd9\u4e2a\u5b57\u7b26\u4e32\u76f8\u5e94\u7684\u500d\u6570\u3002\u4f8b\u5982\uff0c<code>\"tar\"*2<\/code>\u662f<code>\"tartar\"<\/code>\u3002 <\/span><\/td><\/table><\/p>\n<p><em id=\"funcfunc\">\u5411\u5de6\u6216\u5411\u53f3\u62d6\u52a8\u5782\u76f4\u7684\u7070\u7ebf\u6765\u6539\u53d8\u53ef\u89c6\u5316\u548c\u4ee3\u7801\u663e\u793a\u7684\u591a\u5c11\u3002<\/em><\/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=def+inner%28string%2C+width%29%3A%0A++return+string+%2B+%22+%22+%2A+%28width+-+len%28string%29%29%0A%0Adef+outer%28string1%2C+string2%29%3A%0A++w+%3D+max%28len%28string1%29%2C+len%28string2%29%29%0A++print%28%22%2A%22+%2A+%28w+%2B+4%29%29%0A++print%28%22%2A+%22+%2B+inner%28string1%2C+w%29+%2B+%22+%2A%22%29%0A++print%28%22%2A+%22+%2B+inner%28string2%2C+w%29+%2B+%22+%2A%22%29%0A++print%28%22%2A%22+%2A+%28w+%2B+4%29%29%0A%0Aouter%28%22%E4%B8%80%E5%8F%B7%E7%AE%B1%E5%AD%90%22%2C+%22%E6%98%AF%E5%8D%81%E5%88%86%E6%9C%89%E8%B6%A3%E7%9A%84%22%29%0Aouter%28%22%E7%AE%B1%E5%AD%90+%232%22%2C+%22%28%E4%B8%8D%E6%98%AFFALSE%29+%E6%98%AF+%22+%2B+str%28not+False%29%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=400&rightStdout=1'><\/iframe><\/p>\n<p>\u5728\u53ef\u89c6\u5316\u4e2d\u4f60\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u65b0\u7684\u73b0\u8c61: \u5728\"\u5e27\"\u8fd9\u5217\u4e0b\uff0c\u5f53\u6211\u4eec\u5728\u6267\u884c30\u6b65\u4e2d\u7684\u7b2c\u516b\u6b65\u65f6\uff0c\u8fd9\u91cc\u4e0d\u53ea\u6709\u4e00\u4e9b\u5168\u5c40\u53d8\u91cf\uff0c\u540c\u6837\u4e5f\u6709\u4e24\u4e2a\u5e27\uff08\u4e00\u4e2a\u662f\u4e3a\u4e86 <code>outer<\/code>\uff0c\u53e6\u4e00\u4e2a\u662f\u4e3a\u4e86\u521a\u5efa\u7acb\u7684<code>inner<\/code>\uff09\u3002\u8fd9\u4e2a<code>outer<\/code>\u4f1a\u6682\u505c\u5e76\u7b49\u5f85<code>inner<\/code>\u5b8c\u5168\u7ed3\u675f\uff0c\u7136\u540e <code>outer<\/code>\u4f1a\u91cd\u65b0\u5f00\u59cb\u3002\u63a5\u7740\u5b83\u4f1a\u518d\u8c03\u7528\u4e00\u904d<code>inner<\/code>\uff0c\u540c\u6837<code>outer<\/code>\u8fd8\u662f\u7b49\u5f85\u7740\uff1b\u5f53<code>inner<\/code>\u5b8c\u6210\u65f6\uff0c<code>outer<\/code>\u4f1a\u63a5\u7740\u5b8c\u6210\u3002\u6700\u7ec8\uff0c\u6211\u4eec\u8c03\u7528<code>outer<\/code>\u7684\u6574\u4e2a\u8fc7\u7a0b\u53c8\u4f1a\u63a5\u7740\u91cd\u590d\u3002<\/p>\n<p>\u4f60\u73b0\u5728\u53ef\u4ee5\u8fdb\u5165\u4e0b\u4e00\u8bfe\u4e86!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u5411\u4f60\u5c55\u793a\u7f16\u7a0b\u4e2d\u6700\u91cd\u8981\u7684\u5185\u5bb9\uff1a\u5b9a\u4e49\u51fd\u6570\uff01\u51fd\u6570\u8ba9\u4f60\u7684\u7a0b\u5e8f\u53d8\u5f97\u66f4\u77ed, \u66f4\u6574\u6d01, \u4fbf\u4e8e\u9605\u89c8\u548c\u8c03\u8bd5\uff0c\u63d0\u9ad8\u53ef\u91cd\u590d\u4f7f\u7528\u6027\u3002\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u89c1\u5230\u5176\u4ed6\u7684\u597d\u5904\uff0c\u4f8b\u5982\u9012\u5f52\u3002 \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570 \u6211\u4eec\u4f1a\u4ece\u4e00\u4e2a\u4f8b\u5b50\u5f00\u59cb\u3002\u8fd8\u8bb0\u5f97abs\u8fd9\u4e2a\u51fd\u6570\u4e48? \u5b83\u8bfb\u53d6\u4e00\u4e2a\u5b9e\u53c2\uff08\u4e00\u4e2a\u6570\u5b57 x\uff09\uff0c\u7136\u540e\u8fd4\u8fd8\u5b83\u7684\u7edd\u5bf9\u503c\uff08\u5f53x \u2265 0\uff0c\u7b49\u4e8ex\uff1b\u5f53 x &lt; 0\uff0c\u7b49\u4e8e-x\uff09\u3002\u5728Python\u4e2d\uff0c\u5b83\u662f\u8fd9\u6837\u5b9a\u4e49\u8fd9\u4e2a\u51fd\u6570\u7684: def abs(x): # \u5b9a\u4e49\u4e00\u4e2a\u5e26\u6709\u4e00\u4e2a\u5b9e\u53c2\"x\"\u7684\u51fd\u6570\u201cabs\u201d if x &gt;= 0: # \u51fd\u6570\u4ece\u8fd9\u91cc\u5f00\u59cb return x else: return -x # \u51fd\u6570\u5728\u8fd9\u91cc\u7ed3\u675f(\u70b9\u51fb\u8fd9\u91cc\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e2d\u6d4b\u8bd5\u8fd9\u6bb5\u4ee3\u7801\u3002) \u51fd\u6570\u5b9a\u4e49\u7684\u4e24\u4e2a\u90e8\u5206 \u51fd\u6570\u5b9a\u4e49\u7684\u7b2c\u4e00\u884c\u786e\u5b9a\u4e86\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u5b57\u548c\u5b9e\u53c2\u3002\u5b83\u5e94\u8be5\u770b\u4e0a\u53bb\u50cf\u8fd9\u6837\uff1a def \u00ab\u51fd\u6570\u540d\u5b57\u00bb(\u00ab\u5b9e\u53c2\u5217\u00bb): \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d, abs\u662f\u51fd\u6570\u7684\u540d\u5b57\uff0c\u5e76\u4e14\u5b9e\u53c2\u5217\u662fx\u3002\u4e4b\u6240\u4ee5\u79f0\u4e4b\u4e3a\u5b9e\u53c2\u5217\u662f\u56e0\u4e3a\u8fd9\u91cc\u53ef\u4ee5\u6709\u591a\u4e2a\u5b9e\u53c2\uff0c\u6bd4\u5982x, y, z; \u540c\u6837\uff0c\u6ca1\u6709\u5b9e\u53c2\u4e5f\u662f\u53ef\u884c\u7684, \u4f8b\u5982\u4e00\u4e2a\u7a7a\u7684\u5217\u3002\u8f93\u5165\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u7684\u5b9e\u53c2\u3002 \u6240\u6709\u5728\u51fd\u6570\u5b9a\u4e49\u7b2c\u4e00\u884c\u4e4b\u540e\u7684\u5185\u5bb9\u90fd\u662f\u51fd\u6570\u4f53\u3002\u51fd\u6570\u4f53\u662f\u4e00\u5757\u7f29\u8fdb\u7684\u4ee3\u7801\u3002\u6bcf\u5f53\u4e00\u4e2a\u51fd\u6570\u88ab\u8c03\u7528, &hellip; <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-10\/\">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-9296","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9296","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=9296"}],"version-history":[{"count":10,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9296\/revisions"}],"predecessor-version":[{"id":10223,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9296\/revisions\/10223"}],"wp:attachment":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/media?parent=9296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}