{"id":9298,"date":"2016-06-07T23:30:01","date_gmt":"2016-06-08T03:30:01","guid":{"rendered":"http:\/\/cscircles.cemc.uwaterloo.ca\/?page_id=9298"},"modified":"2017-06-28T18:39:38","modified_gmt":"2017-06-28T22:39:38","slug":"zh-11b","status":"publish","type":"page","link":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-11b\/","title":{"rendered":"11B: \u53d8\u91cf\u7684\u4f5c\u7528\u57df"},"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><em>\u7b2c11\u8bfe\u6709\u4e09\u4e2a\u90e8\u5206A\u3001B\u3001C\uff0c\u5b66\u4e60\u987a\u5e8f\u4e0d\u5206\u5148\u540e\u3002<\/em><\/p>\n<p>\u5728\u8fd9\u8282\u8bfe\u4e2d\u6211\u4eec\u4f1a\u89e3\u91ca\u4e00\u4e2a\u53eb\u505a<em>\u53d8\u91cf\u4f5c\u7528\u57df<\/em>\u7684\u6982\u5ff5\u3002\u5927\u610f\u5c31\u662f\u4f7f\u4e24\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\uff0c<br \/>\n\u5728\u5176\u4f5c\u7528\u57df\u4e0d\u540c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u540c\u4e00\u4e2a\u540d\u5b57\u3002\u8fd9\u5c06\u4f7f\u7f16\u5199\u548c\u4fee\u6539\u7a0b\u5e8f\u66f4\u52a0\u7b80\u5355\uff0c\u5c24\u5176\u662f\u90a3\u4e9b\u5e9e\u5927\u7684\u7a0b\u5e8f\u3002\u540c\u65f6\uff0c\u5b83\u4e5f\u662f\u6211\u4eec\u4ee5\u540e\u4f1a\u5b66\u5230\u7684\u5185\u5bb9\uff08\u9012\u5f52\uff09\u7684\u4e00\u4e2a\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002<\/p>\n<h2>\u53d8\u91cf\u4f5c\u7528\u57df\u7684\u793a\u4f8b<\/h2>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d,\uff0c\u6211\u4eec\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\u4f1a\u6709\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\uff1a<\/p>\n<pre>def square(x):\r\n   value = x * x\r\n   return value<\/pre>\u8fd9\u662f\u4e00\u4e2a\u51fd\u6570\u7528\u6765\u8ba1\u7b97\u4e00\u4e2a\u6570<code>x<\/code>\u7684\u5e73\u65b9\uff0c\u4f8b\u5982 <code>square(5)<\/code> \u4f1a\u8fd4\u8fd8<code>25<\/code>\u3002\u73b0\u5728\u5047\u8bbe\u6211\u4eec\u4ece\u7a0b\u5e8f\u7684\u53e6\u4e00\u4e2a\u90e8\u5206\u8c03\u7528<code>square<\/code>\u51fd\u6570\uff0c\u8fd9\u4e2a\u90e8\u5206\u7684\u7a0b\u5e8f\u4e3a\u4e86\u53e6\u4e00\u4e2a\u76ee\u7684\u4e5f\u4f7f\u7528\u4e86value \u8fd9\u4e2a\u53d8\u91cf\uff1a<\/p>\n<pre># \u51fd\u6570\u4f53\u4ece\u8fd9\u91cc\u5f00\u59cb\r\nvalue = 17\r\nfivesquared = square(5)\r\nprint(fivesquared, value)<\/pre>\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7a0b\u5e8f\u4f1a\u6253\u5370\u51fa\u4ec0\u4e48\uff1f\u5047\u8bbe\u6211\u4eec\u4e0d\u77e5\u9053Python\u662f\u600e\u6837\u5de5\u4f5c\u7684\uff0c\u90a3\u4e48\u6709\u4e24\u79cd\u53ef\u80fd\uff1a<\/p>\n<ol>\n<li>\u4e00\u79cd\u53ef\u80fd\u662fPython\u89c9\u5f97\u4e24\u4e2a<code>value<\/code>\u53d8\u91cf\u5e94\u8be5\u88ab\u201c\u5206\u522b\u4fdd\u5b58\u201d\u3002\u90a3\u4e48\uff0c\u8c03\u7528<code>square<\/code>\u4f1a\u8fd4\u8fd825\uff0c\u4f46\u662f<code>value<\/code>\u5728main\u4e3b\u4f53\u4e2d\u7684\u503c\u4f1a\u4fdd\u7559\u572817\uff0c\u540c\u65f6\u6211\u4eec\u4f1a\u770b\u89c1\u8f93\u51fa<code>25 <\/code><code>17<\/code>\u3002<\/li>\n<li>\u53e6\u4e00\u79cd\u53ef\u80fd\u662f\u5f53<code>square<\/code>\u88ab\u6267\u884c\u540e\uff0c\u5b83\u5c06\u73b0\u6709\u7684<code>value<\/code>\u7684\u503c\u6539\u5199\u4e3a25, \u6240\u4ee5\u6211\u4eec\u4f1a\u770b\u5230\u8f93\u51fa<code>25 <\/code><code>25<\/code>\u3002<\/li>\n<\/ol>\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48\uff01<\/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%0A++++value+%3D+x+%2A+x%0A++++return+value%0A%0A%23+%E7%A8%8B%E5%BA%8F%E4%B8%BB%E4%BD%93%E4%BB%8E%E8%BF%99%E9%87%8C%E5%BC%80%E5%A7%8B%0Avalue+%3D+17%0Afivesquared+%3D+square%285%29%0Aprint%28fivesquared%2C+value%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=450&rightStdout=1'><\/iframe><\/p>\n<p>Python\u7684\u8fd0\u884c\u5b9e\u9645\u4e0a\u7b26\u5408\u7b2c\u4e00\u79cd\u3002\u4e8b\u5b9e\u4e0a, <em>\u6bcf\u5f53\u4f60\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u4f60\u65e0\u6cd5\u6539\u53d8\u4efb\u4f55\u5728\u51fd\u6570\u5916\u5b9a\u4e49\u7684\u53d8\u91cf<\/em>\u3002\u7ed9\u53d8\u91cf\u8d4b\u503c\u7684\u4efb\u4f55\u8bed\u8a00\u53ea\u5f71\u54cd\u4e00\u4e2a\u8c03\u7528\u51fd\u6570\u4e2d\u7684\u201c\u5c40\u90e8\u201d\u53d8\u91cf\uff0c\u5b83\u53ea\u8c03\u7528\u51fd\u6570\u7684\u4e00\u4e2a\u201c\u5185\u90e8\u201d\u53d8\u91cf\u3002Python\u548c\u5927\u591a\u6570\u5176\u4ed6\u8bed\u8a00\u8fd9\u4e48\u505a\u7684<strong>\u539f\u56e0<\/strong>\u662f\u8fd9\u6837\u53ef\u4ee5\u5728\u7f16\u5199\u5927\u578b\u7a0b\u5e8f\u3001\u9762\u5bf9\u6210\u5343\u4e0a\u767e\u7684\u51fd\u6570\u65f6\uff0c\u8ba9\u5de5\u4f5c\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u65b9\u4fbf\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5f53\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u4efb\u4f55\u53d8\u91cf\u540d(\u5305\u62ec\u5e38\u89c1\u7684\u540d\u79f0 <code>result,<\/code> <code>temp<\/code> \u548c <code>i<\/code>) \u5373\u4f7f\u4ed6\u4eec\u5728\u5176\u4ed6\u5730\u65b9\u88ab\u7528\u4e8e\u4e0d\u540c\u7684\u76ee\u7684\u3002<\/p>\n<p>\u8be6\u7ec6\u6765\u770b\u3002\u5728\u4ee3\u7801\u5c55\u793a\u53f0\u7684\u7b2c\u516d\u6b65\u4e2d\uff0c\u4f60\u4f1a\u6ce8\u610f\u5230\u8fd9\u91cc\u6709\u4e24\u4e2a\u4e0d\u540c\u7684<code>value<\/code>\u53d8\u91cf: \u4e00\u4e2a\u5728<code>square<\/code>\u51fd\u6570\u4e2d, \u53e6\u4e00\u4e2a\u5728\u51fd\u6570\u5916\u9762\uff08\u5728\u5168\u5c40\u4e2d\uff09\u3002<\/p>\n<div class='pybox modeNeutral' id='pybox0'>\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'>\u5185\u90e8\u4e0e\u5916\u90e8<\/span><\/div><div>\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u8f93\u51fa\u662f\u4ec0\u4e48\uff1f<\/p>\n<pre>x = \"\u5916\u90e8\"\r\ndef xReplace(value):\r\n   x = value\r\nxReplace(\"\u5185\u90e8\")\r\nprint(x)<\/pre><\/div><label>Your choice: <\/label><select id=\"pyselect0\"><option value=\"d\" selected>Select one<\/option><option value=\"w\">\u7a0b\u5e8f\u9020\u6210\u4e86\u4e00\u4e2a\u9519\u8bef\u3002<\/option><option value=\"w\">\u5185\u90e8<\/option><option value=\"r\">\u5916\u90e8<\/option><\/select><div class='pyboxbuttons'><input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" name=\"slug\" value=\"11b.inout\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick='pbMultiCheck(0)'\/><\/div><div class=\"pbresults\" id=\"pyMultiResults0\"><\/div><div class=\"epilogue\">\u6b63\u786e! \u8bed\u53e5<code>x = value<\/code>\u53ea\u4f1a\u5f71\u54cd\u51fd\u6570\u4e2d\u5c40\u90e8\u7c7b\u578b\u7684\u53d8\u91cf<code>x<\/code>\u3002\u5168\u5c40\u7c7b\u578b\u7684 <code>x<\/code>\u4e0d\u4f1a\u6539\u53d8(\u6240\u4ee5\uff0c\u51fd\u6570 <code>xReplace<\/code> \u5e76\u6ca1\u6709\u4efb\u4f55\u7528\u5904\uff0c\u4e5f\u4e0d\u4f1a\u9020\u6210\u4efb\u4f55\u5f71\u54cd)\u3002<\/div><\/div>\n<p>\u53e6\u4e00\u4e2a\u548c<em>\u4f5c\u7528\u57df<\/em>\u7c7b\u4f3c\u7684\u6982\u5ff5\u662f<em>\u547d\u540d\u7a7a\u95f4<\/em>\uff1b\u547d\u540d\u7a7a\u95f4\u5c31\u50cf\u662f\u4e00\u4e2a\u5305\u542b\u4f5c\u7528\u57df\u7684\u5305\u88f9\u3002\u6240\u4ee5\u5373\u4f7f\u4f60<code>import<\/code>\u4e00\u4e2a\u5305\u88f9 (\u4f8b\u5982 <code>math<\/code>\uff09\uff0c\u5728\u4e00\u4e9b\u5730\u65b9\u7528\u5230\u53d8\u91cf\u540d<code>x<\/code>\uff0c\u5b83\u4e5f\u4e0d\u4f1a\u4e0e\u4f60\u7684\u7a0b\u5e8f\u4f7f\u7528\u7684\u53d8\u91cf<code>x<\/code>\u91cd\u53e0, \u56e0\u4e3a\u5b83\u4eec\u5728\u4e0d\u540c\u7684\u547d\u540d\u7a7a\u95f4\u91cc\u3002<\/p>\n<h2>\u4f5c\u7528\u57df\u89c4\u5219\uff1a\u5411\u5916\u5c55\u671b<\/h2>\n<p>\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u60f3\u6df7\u5408\u5c40\u90e8\u548c\u5168\u5c40\u7684\u53d8\u91cf\u3002\u4e00\u4e2a\u5e38\u89c1\u7684\u4f8b\u5b50\u662f\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u6709\u4e00\u4e9b\u5e38\u89c1\u7684\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u4f46\u4f60\u4e5f\u5e0c\u671b\u4f60\u7684\u51fd\u6570\u80fd\u591f\u8bfb\u53d6\u8fd9\u4e9b\u5168\u5c40\u53d8\u91cf\u3002<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform1\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox1'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u8bfb\u53d6\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode1' id='usercode1'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\nfavouriteTopping='pepperoni'\ndef orderPizzas(n):\n print(\"I'd like\", str(n), favouriteTopping, \"pizzas\")\norderPizzas(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=\"6283e6e1d383f13a78a4aaa39e5b6aad\"\/>\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<\/p>\n<p>\u5728\u8fd9\u91cc, \u5c40\u90e8\u4f5c\u7528\u57df\u5e76\u4e0d\u542b\u6709\u4e00\u4e2a\u53eb\u4f5c<code>favouriteTopping<\/code>\u7684\u53d8\u91cf<em>\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u662f\u9020\u6210\u9519\u8bef\u7684\u539f\u56e0<\/em>\u3002Python\u8bc4\u4f30\u53d8\u91cf\u7684\u89c4\u5219\u662f\uff0c\u5982\u679c\u4e00\u4e2a\u4e0d\u5728\u5c40\u90e8\u4f5c\u7528\u57df\u91cc\u7684\u53d8\u91cf\u9700\u8981\u88ab\u8bc4\u4f30\uff0c\u90a3\u4e48\u5b83\u4f1a\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u5bfb\u627e\u6b64\u53d8\u91cf\u3002\u5728\u6211\u4eec\u7684\u6848\u4f8b\u4e2d\uff0c\u5b83\u786e\u5b9e\u5728\u5168\u5c40\u4e2d\u627e\u5230\u4e86<code>favouriteTopping<\/code>\uff08\u5728\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7528\u4e00\u4e2a\u51fd\u6570\u4f53\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u4f60\u4f1a\u78b0\u5230\u4e09\u5c42\u4f5c\u7528\u57df\uff1bPython\u603b\u662f\u5148\u68c0\u67e5\u201c\u6700\u5c40\u90e8\u7684\u201d\uff0c\u63a5\u7740\u671d\u5168\u5c40\u4f5c\u7528\u57df\u201c\u4e00\u6b65\u4e00\u6b21\u201d\u7684\u524d\u8fdb\uff0c\u76f4\u5230\u53d1\u73b0\u8fd9\u4e2a\u53d8\u91cf\u3002\uff09\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> \u4e0a\u9762\u7684\u4e24\u4e2a\u4f8b\u5b50\uff0c<code>orderPizzas<\/code>\u548c<code>xReplace<\/code>\u5728\u8bed\u6cd5\u4e0a\u57fa\u672c\u76f8\u540c\u3002\u4e3a\u4ec0\u4e48Python\u5728<code>xReplace<\/code>\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf<code>x<\/code>\uff0c\u4f46\u6ca1\u6709\u5728 <code>orderPizzas<\/code>\u4e2d\u521b\u5efa\u65b0\u7684<code>favouriteTopping<\/code>\uff1fPython\u7684\u89c4\u5219\u5982\u4e0b\uff1a\u5982\u679c\u4f60\u53ea\u8bfb\u53d6\u8fd9\u4e2a\u53d8\u91cf\uff0c\u90a3\u4e48\u6ca1\u6709\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\u4f1a\u88ab\u521b\u5efa\uff1b\u4f46\u662f\u4f60\u54ea\u6015\u53ea\u662f\u5bf9\u8fd9\u4e2a\u53d8\u91cf\u5199\u5165\u4e86\u4e00\u6b21\uff0c\u8fd9\u4e2a\u53d8\u91cf\u4e5f\u4f1a\u5728\u51fd\u6570\u4f53\u4e2d\u88ab\u5f53\u6210\u5c40\u90e8\u53d8\u91cf\u5bf9\u5f85\uff08\u8fd9\u662f\u9020\u6210\u4e0b\u9762\u8fd9\u79cd\u60c5\u51b5\u7684\u6700\u5e38\u89c1\u539f\u56e0 <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/console\/?consolecode=x%20%3D%201%0Adef%20f%28%29%3A%0A%20print%28x%29%0A%20x%20%3D%202%0Af%28%29\" target=\"_blank\"><code>UnboundLocalError<\/code><\/a>: \u8bf7\u770bPython\u5b98\u65b9\u6587\u6863\u4e2d\u7684\u8fd9\u4e24\u4e2a\u95ee\u9898\u3002[<a href=\"http:\/\/docs.python.org\/py3k\/faq\/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value\" target=\"_blank\">1<\/a>, <a href=\"http:\/\/docs.python.org\/py3k\/faq\/programming.html#what-are-the-rules-for-local-and-global-variables-in-python\" target=\"_blank\">2<\/a>]\uff09 <\/span><\/td><\/table><\/p>\n<p>\u51fd\u6570\u5b9e\u53c2<em>forever<\/em>\u4f1a\u88ab\u5f53\u4f5c\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u8bf4\u660e\u4e86\u8fd9\u4e2a\u89c4\u5219\uff08\u5b83\u5728\u5c1d\u8bd5\u91cd\u7f6e\u53d8\u91cf<code>g<\/code>\u81f3<code>0<\/code>\uff0c\u4f46\u5931\u8d25\u4e86)\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+resetToZero%28var%29%3A%0A+var+%3D+0%0Ag+%3D+15%0AresetToZero%28g%29%0Aprint%28g%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<h2><code>global<\/code>\u6539\u53d8<\/h2>\n<p>\u548c\u5176\u4ed6\u5f88\u591a\u4e8b\u4e00\u6837, \u4e0a\u9762\u63cf\u8ff0\u7684\u572899%\u7684\u60c5\u51b5\u4e0b\u90fd\u662f\u6b63\u786e\u7684\u3002\u4f46\u662f\u5728\u5269\u4e0b\u76841%\u60c5\u51b5\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c1d\u8bd5\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u6539\u53d8\u4e00\u4e2a\u5168\u7403\u53d8\u91cf\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684<code>global<\/code>\u8bed\u53e5\u6765\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5bf9\u65e9\u671f\u7684<code>xReplace<\/code>\u793a\u4f8b\u7684\u4fee\u6539\uff1b\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\u4e3a<code>global<\/code>\u53ef\u4ee5\u8ba9\u4f60\u4ece\u51fd\u6570\u5185\u90e8\u66f4\u6539\u5168\u5c40\u503c\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+xReplace%28value%29%3A%0A++++global+x%0A++++x+%3D+value%0A%0Ax+%3D+%22outer%22%0AxReplace%28%22inner%22%29+%0Aprint%28x%29+++++++%23+%E7%8E%B0%E5%9C%A8%E6%98%AF%27inner%27%21&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>\u6211\u4eec\u4e4b\u524d\u63d0\u5230\u8fc7, <em>\u8bfb\u53d6<\/em>\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u4e0d\u9700\u8981\u4f7f\u7528\u5230<code>global<\/code>\u8bed\u53e5; \u53ea\u6709\u5728<em>\u5199\u5165<\/em>\u65f6\u9700\u8981\u7528\u5230\u3002\u8fd9\u5c31\u662f\u8fd9\u8282\u8bfe\u7684\u5185\u5bb9!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b2c11\u8bfe\u6709\u4e09\u4e2a\u90e8\u5206A\u3001B\u3001C\uff0c\u5b66\u4e60\u987a\u5e8f\u4e0d\u5206\u5148\u540e\u3002 \u5728\u8fd9\u8282\u8bfe\u4e2d\u6211\u4eec\u4f1a\u89e3\u91ca\u4e00\u4e2a\u53eb\u505a\u53d8\u91cf\u4f5c\u7528\u57df\u7684\u6982\u5ff5\u3002\u5927\u610f\u5c31\u662f\u4f7f\u4e24\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\uff0c \u5728\u5176\u4f5c\u7528\u57df\u4e0d\u540c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u540c\u4e00\u4e2a\u540d\u5b57\u3002\u8fd9\u5c06\u4f7f\u7f16\u5199\u548c\u4fee\u6539\u7a0b\u5e8f\u66f4\u52a0\u7b80\u5355\uff0c\u5c24\u5176\u662f\u90a3\u4e9b\u5e9e\u5927\u7684\u7a0b\u5e8f\u3002\u540c\u65f6\uff0c\u5b83\u4e5f\u662f\u6211\u4eec\u4ee5\u540e\u4f1a\u5b66\u5230\u7684\u5185\u5bb9\uff08\u9012\u5f52\uff09\u7684\u4e00\u4e2a\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002 \u53d8\u91cf\u4f5c\u7528\u57df\u7684\u793a\u4f8b \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d,\uff0c\u6211\u4eec\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\u4f1a\u6709\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\uff1a def square(x): value = x * x return value\u8fd9\u662f\u4e00\u4e2a\u51fd\u6570\u7528\u6765\u8ba1\u7b97\u4e00\u4e2a\u6570x\u7684\u5e73\u65b9\uff0c\u4f8b\u5982 square(5) \u4f1a\u8fd4\u8fd825\u3002\u73b0\u5728\u5047\u8bbe\u6211\u4eec\u4ece\u7a0b\u5e8f\u7684\u53e6\u4e00\u4e2a\u90e8\u5206\u8c03\u7528square\u51fd\u6570\uff0c\u8fd9\u4e2a\u90e8\u5206\u7684\u7a0b\u5e8f\u4e3a\u4e86\u53e6\u4e00\u4e2a\u76ee\u7684\u4e5f\u4f7f\u7528\u4e86value \u8fd9\u4e2a\u53d8\u91cf\uff1a # \u51fd\u6570\u4f53\u4ece\u8fd9\u91cc\u5f00\u59cb value = 17 fivesquared = square(5) print(fivesquared, value)\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7a0b\u5e8f\u4f1a\u6253\u5370\u51fa\u4ec0\u4e48\uff1f\u5047\u8bbe\u6211\u4eec\u4e0d\u77e5\u9053Python\u662f\u600e\u6837\u5de5\u4f5c\u7684\uff0c\u90a3\u4e48\u6709\u4e24\u79cd\u53ef\u80fd\uff1a \u4e00\u79cd\u53ef\u80fd\u662fPython\u89c9\u5f97\u4e24\u4e2avalue\u53d8\u91cf\u5e94\u8be5\u88ab\u201c\u5206\u522b\u4fdd\u5b58\u201d\u3002\u90a3\u4e48\uff0c\u8c03\u7528square\u4f1a\u8fd4\u8fd825\uff0c\u4f46\u662fvalue\u5728main\u4e3b\u4f53\u4e2d\u7684\u503c\u4f1a\u4fdd\u7559\u572817\uff0c\u540c\u65f6\u6211\u4eec\u4f1a\u770b\u89c1\u8f93\u51fa25 17\u3002 \u53e6\u4e00\u79cd\u53ef\u80fd\u662f\u5f53square\u88ab\u6267\u884c\u540e\uff0c\u5b83\u5c06\u73b0\u6709\u7684value\u7684\u503c\u6539\u5199\u4e3a25, \u6240\u4ee5\u6211\u4eec\u4f1a\u770b\u5230\u8f93\u51fa25 25\u3002 \u8ba9\u6211\u4eec\u770b\u770b\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48\uff01 Python\u7684\u8fd0\u884c\u5b9e\u9645\u4e0a\u7b26\u5408\u7b2c\u4e00\u79cd\u3002\u4e8b\u5b9e\u4e0a, \u6bcf\u5f53\u4f60\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u4f60\u65e0\u6cd5\u6539\u53d8\u4efb\u4f55\u5728\u51fd\u6570\u5916\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u7ed9\u53d8\u91cf\u8d4b\u503c\u7684\u4efb\u4f55\u8bed\u8a00\u53ea\u5f71\u54cd\u4e00\u4e2a\u8c03\u7528\u51fd\u6570\u4e2d\u7684\u201c\u5c40\u90e8\u201d\u53d8\u91cf\uff0c\u5b83\u53ea\u8c03\u7528\u51fd\u6570\u7684\u4e00\u4e2a\u201c\u5185\u90e8\u201d\u53d8\u91cf\u3002Python\u548c\u5927\u591a\u6570\u5176\u4ed6\u8bed\u8a00\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u662f\u8fd9\u6837\u53ef\u4ee5\u5728\u7f16\u5199\u5927\u578b\u7a0b\u5e8f\u3001\u9762\u5bf9\u6210\u5343\u4e0a\u767e\u7684\u51fd\u6570\u65f6\uff0c\u8ba9\u5de5\u4f5c\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u65b9\u4fbf\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5f53\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u4efb\u4f55\u53d8\u91cf\u540d(\u5305\u62ec\u5e38\u89c1\u7684\u540d\u79f0 result, temp \u548c i) \u5373\u4f7f\u4ed6\u4eec\u5728\u5176\u4ed6\u5730\u65b9\u88ab\u7528\u4e8e\u4e0d\u540c\u7684\u76ee\u7684\u3002 &hellip; <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-11b\/\">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-9298","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9298","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=9298"}],"version-history":[{"count":14,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9298\/revisions"}],"predecessor-version":[{"id":10226,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9298\/revisions\/10226"}],"wp:attachment":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/media?parent=9298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}