{"id":2450,"date":"2025-09-07T21:06:19","date_gmt":"2025-09-07T13:06:19","guid":{"rendered":"http:\/\/blue.yn.cn\/?p=2450"},"modified":"2025-11-22T11:56:34","modified_gmt":"2025-11-22T03:56:34","slug":"iterations","status":"publish","type":"post","link":"http:\/\/blue.yn.cn\/?p=2450","title":{"rendered":"5. Iterations"},"content":{"rendered":"<p>Updating variables<\/p>\n<p>A common pattern in assignment statements is an assignment statements that updates a variable, where the new value of the variable depends on the old.<\/p>\n<pre><code class=\"language-python\">x = x + 1<\/code><\/pre>\n<p>This means &quot;get the current value of <code>x<\/code>, add 1, and then update <code>x<\/code> with the new value.&quot;<\/p>\n<p>If you try to update a variable that doesn't exist, you get an error, because Python evaluates the right side before it assigns a value to <code>x<\/code>:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; x = x + 1\nNameError: name &#039;x&#039; is not defined<\/code><\/pre>\n<p>Before you can update a variable, you have to initialize it, usually with a simple assignment:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; x = 0\n>&gt;&gt; x = x + 1<\/code><\/pre>\n<p>Updating a variable by adding 1 is called an increment; subtracting 1 is called a decrement.<\/p>\n<h3>The <code>while<\/code> statement<\/h3>\n<p><strong>while<\/strong><\/p>\n<p>Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that computers do well and people do poorly. Because iteration is so common, Python provides several language features to make it easier.<\/p>\n<p>One form of iteration in Python is the <code>while<\/code> statements. Here is a simple program that counts down from five and then says &quot;Blastoff!&quot;.<\/p>\n<pre><code class=\"language-python\">n = 5\nwhile n &gt; 0:\n    print(n)\n    n = n - 1\nprint(&#039;Blastoff!&#039;)<\/code><\/pre>\n<p>You can almost read the <code>while<\/code> statements as if it were English. It means, &quot;While <code>n<\/code> is greater than 0, display the value of <code>n<\/code> and then reduce the value of <code>n<\/code> by 1. When you get to 0, exit the <code>while<\/code> statement and display the word <code>Blastoff!<\/code>&quot;<\/p>\n<p>More formally, here is the flow of execution for a <code>while<\/code> statement:<\/p>\n<ol>\n<li>Evaluate the condition, yielding <code>True<\/code> or <code>False<\/code>.<\/li>\n<li>If the condition is false, exit the <code>while<\/code> statement and continue execution at the next statement.<\/li>\n<li>If the condition is True, execute the body and then go back to step 1.<\/li>\n<\/ol>\n<p>This type of flow is called a loop because the third step loops back around to the top. We call each time we execute the body of the loop an iteration. For the above loop, we would say, &quot;It had five iterations&quot;, which means that the body of loop was executed five times.<\/p>\n<p>The body of loop should change the value of one or more variables so that eventually the condition becomes false and the loop terminates. We call the variable that changes each time the loop executes and controls the loop finishes the iteration variable. If there is no iteration variable, the loop will repeat forever, resulting in an infinite loop.<\/p>\n<h3>Infinite loops<\/h3>\n<p>An endless source of amusement for programmers is the observation that the direction on shampoo, &quot;Lather, rinse, repeat,&quot; are an infinite loop because there is no iteration variable telling you how many times to execute the loop.<\/p>\n<p>In the case of <code>countdown<\/code>, we can prove that the loop terminates because we know that the value of <code>n<\/code> is finite, and we can see that the value of <code>n<\/code> gets smaller each time through the loop, so eventually we have to get to 0. Other times a loop is obviously infinite because it has no iteration variable at all.<\/p>\n<p>Sometimes you don't know it's time to end a loop until you get half way through the body. In that case you can write an infinite loop on purpose and then use the <code>break<\/code> statement to jump out of the loop.<\/p>\n<p>This loop is obviously an infinite loop because the logical expression on the <code>while<\/code> statement is simply the logical constant <code>True<\/code>:<\/p>\n<pre><code class=\"language-python\">n = 10\nwhile True:\n    print(n, end=&#039; &#039;)\n    n = n - 1\nprint(&#039;Done!&#039;)<\/code><\/pre>\n<p>If you make the mistake and run this code, you will learn quickly how to stop a runaway Python process on your system or find where the power-off button is on your computer. This program will run forever or until your battery runs out because the logical expression at the top of the loop is always true by virtue of the fact that the expression is the constant value <code>True<\/code>.<\/p>\n<p>While this is dysfunctional infinite loop, we can still use this pattern to build useful loops as long as we carefully add code to the body of the loop to explicitly exit the loop using <code>break<\/code> when we have reached the exit condition.<\/p>\n<p>For example, suppose you want to take input from the user until they type <code>done<\/code>. You could write:<\/p>\n<pre><code class=\"language-python\">while True:\n    line = input(&#039;&gt; &#039;)\n    if line == &#039;done&#039;:\n        break\n    print(line)\nprint(&#039;Done!&#039;)<\/code><\/pre>\n<p>The loop condition is <code>True<\/code>, which is always true, so the loop runs repeatedly until it hits the break statement.<\/p>\n<p>Each time through, it prompts the user with an angle bracket. If the user type <code>done<\/code>, the <code>break<\/code> statement exits the loop. Otherwise the program echoes whatever the user types and goes back to the top of the loop. Here's a sample run:<\/p>\n<pre><code class=\"language-python\">> hello there\nhello there\n> finished\nfinished\n> done\nDone!<\/code><\/pre>\n<p>This way of writing <code>while<\/code> loops is common because you can check the condition anywhere in the loop ( not just at the top) and you can express the stop condition affirmatively (&quot;stop when this happens&quot;) rather than negatively (&quot;keep going until that happens.&quot;).<\/p>\n<h3>Finishing iterations with <code>continue<\/code><\/h3>\n<p>Sometimes you are in an iteration of a loop and want to finish the current iteration and immediately jump to the next iteration. In that case you can use the <code>continue<\/code> statement to skip to the next iteration without finishing the body of the loop for the current iteration.<\/p>\n<p>Here is an example of a loop that copies its input until the user types &quot;done&quot;, but treats lines that start with the hash character as lines not to be printed (kind of like Python comments).<\/p>\n<pre><code class=\"language-python\">while True:\n    line = input(&#039;&gt; &#039;)\n    if line[0] == &#039;#&#039;:\n        continue\n    if line == &#039;done&#039;:\n        break\n    print(line)\nprint(&#039;Done!&#039;)<\/code><\/pre>\n<p>Here is a sample run of this new program with <code>continue<\/code> added.<\/p>\n<pre><code class=\"language-python\">> hello there\nhello there\n> # don&#039;t print this\n> print this!\nprint this!\n> done\nDone!<\/code><\/pre>\n<p>All the lines are printed except the one that starts with the hash sign because when the <code>continue<\/code> is executed, it ends the current iteration and jumps back to the <code>while<\/code> statement to start the next iteration, thus skipping the <code>print<\/code> statement.<\/p>\n<h3>Definite loops using <code>for<\/code><\/h3>\n<p>Sometimes we want to loop through a set of things such as a list of words, the lines in a file, or a list of numbers. When we have a list of things to loop through, we can construct a definite loop using a <code>for<\/code> statement. We call the <code>while<\/code> statement an indefinite loop because it simply loops until some condition becomes <code>False<\/code>, whereas the <code>for<\/code> loop is looping through a known set of items so it runs through as many iterations as there are items in the set.<\/p>\n<p>The syntax of a <code>for<\/code> loop is similar to the <code>while<\/code> loop in that there is a <code>for<\/code> statement and a loop body:<\/p>\n<pre><code class=\"language-python\">friends = [&#039;Joseph&#039;, &#039;Glenn&#039;, &#039;Sally&#039;]\nfor friend in friends:\n    print(&#039;Happy New Year:&#039;, friend)\nprint(&#039;Done!&#039;)<\/code><\/pre>\n<p>In Python terms, the variable <code>friends<\/code> is a list of three strings and the <code>for<\/code> loop goes through the list and executes the body once for each of the tree strings in the list resulting in this ouput:<\/p>\n<pre><code class=\"language-python\">Happy New Year: Joseph\nHappy New Year: Glenn\nHappy New Year: Sally\nDone!<\/code><\/pre>\n<p>Translating this <code>for<\/code> loop to English is not as direct as the <code>while<\/code>, but if you think of friends as a set, it goes like this: &quot;Run the statements in the body of the for loop once for each friend in the set named friends.&quot;<\/p>\n<p>Looking at the <code>for<\/code> loop, for and in are reseverd Python keywords, and <code>friend<\/code> and <code>friends<\/code> are variables.<\/p>\n<pre><code class=\"language-python\">for friend in friends:\n    print(&#039;Happy New Year:&#039;, friend)<\/code><\/pre>\n<p>In particular, <code>friend<\/code> is the iteration variable for the for loop. The variable <code>friend<\/code> changes for each iteration of the loop and controls when the <code>for<\/code> loop completes. The iteration variable steps successively through the three strings stored in the <code>friends<\/code> variable.<\/p>\n<h3>Loop patterns<\/h3>\n<p>Often we use a <code>for<\/code> or <code>while<\/code> loop to go through a list of items or the contents of a file and we are looking for something such as the largest or smallest value of the data we scan through.<\/p>\n<p>These loops are generally constructed by:<\/p>\n<ul>\n<li>Initializing one or more variables befor the loop starts<\/li>\n<li>Performing some computation on each item in the loop body, possibly changing the variables in the body of the loop<\/li>\n<li>Looking at the resulting variables when the loop completes<\/li>\n<\/ul>\n<p>We will use a list of numbers to demonstrate the concepts and construction of these loop patterns.<\/p>\n<h3>Counting and summing loops<\/h3>\n<p>For example, to count the number of items in a list, we should write the following <code>for<\/code> loop:<\/p>\n<pre><code class=\"language-python\">count = 0\nfor itervar in [3, 41, 12, 9, 74, 15]:\n    count = count + 1\nprint(&#039;Count: &#039;, count)<\/code><\/pre>\n<p>We set the variable <code>count<\/code> to zero before the loop starts, then we write a <code>for<\/code> loop to run through the list of numbers. Our iteration variable is named <code>itervar<\/code> and while we do not use <code>itervar<\/code> in the loop, it does control the loop and cause the loop body to be executed once for each of the values in the list.<\/p>\n<p>In the body of the loop, we add 1 to the current value of <code>count<\/code> for each of the values in the list. While the loop is executing, the value of <code>count<\/code> is the number of values we have seen &quot;so far&quot;.<\/p>\n<p>Once the loop completes, the value of <code>count<\/code> is the total numbers of items. The total number &quot;falls in our lap&quot; at the end of the loop. We construct the loop so that we have what we want when the loop finishes.<\/p>\n<p>Another similar loop that computes the total of a set of numbers is as follows:<\/p>\n<pre><code class=\"language-python\">total = 0\nfor itervar in [3, 41, 12, 9, 74, 15]:\n    total = total + itervar\nprint(&#039;Total: &#039;, total)<\/code><\/pre>\n<p>In this loop we do use the iteration variable. Instead of simply adding one to the <code>count<\/code> as in the previous loop, we add the actual number (3,41,12,etc.) to the running total during each loop iteration. If you think about the variable <code>total<\/code>, it contains the &quot;running total of the values so far&quot;. So before the loop starts <code>total<\/code> is zero because we have not yet seen any values, during the loop <code>total<\/code> is the running total, and at the end of the loop <code>total<\/code> is the overall total of all the values in the list.<\/p>\n<p>As the loop executes, <code>total<\/code> accumulates the sum of the elements; a variable used this way is sometimes called an accumulator.<\/p>\n<p>Neither the counting loop nor the summing loop are particularly usefull in practice because there are built-in functions <code>len()<\/code> and <code>sum()<\/code> that compute the number of items in a list and the total of the items in the list respectively.<\/p>\n<h3>Maximum and minimum loops<\/h3>\n<p>To find the largest value in a list or sequence, we construct the following loop:<\/p>\n<pre><code class=\"language-python\">largest = None\nprint(&#039;Before:&#039;, largest)\nfor itervar in [3, 41, 12, 9, 74, 15]:\n    if largest is None or itervar &gt; largest :\n        largest = itervar\n    print(&#039;Loop:&#039;, itervar, largest)\nprint(&#039;Largest:&#039;, largest)<\/code><\/pre>\n<p>When the program executes, the output is as follows:<\/p>\n<pre><code class=\"language-python\">Before: None\nLoop: 3 3\nLoop: 41 41\nLoop: 12 41\nLoop: 9 41\nLoop: 74 74\nLoop: 15 74\nLargest: 74<\/code><\/pre>\n<p>The variable <code>largest<\/code> is best thought of as the &quot;largest value we have seen so far&quot;. Befor the loop, we set <code>largest<\/code> to the constant <code>None<\/code>. <code>None<\/code> is a special constant value which we can store in a variable to mark the variable as &quot;empty&quot;.<\/p>\n<p>Before the loop starts, the largest value we have seen so fa is <code>None<\/code> since we have not yet seen any values. While the loop is executing, then we take the first value we see as the largest so far. You can see in the first iteration when the value of <code>itervar<\/code> is 3, since <code>largest<\/code> is <code>None<\/code>, we immediately set <code>largest<\/code> to be 3.<\/p>\n<p>After the first iteration, <code>largest<\/code> is no longer <code>None<\/code>, so the second part of the compound logical expression that checks <code>itervar &gt; largest<\/code> triggers only when we see a value that is larger than the &quot;largest so far&quot;. When we see a new &quot;even larger&quot; value we take that new value for <code>largest<\/code>. You can see in the program output that <code>largest<\/code> progresses from 3 to 41 to 74.<\/p>\n<p>At the end of the loop, we have scanned all of the values and the variable <code>largest<\/code> now does contain the largest value in the list.<\/p>\n<p>To compute the smallest number, the code is very similar with one small change:<\/p>\n<pre><code class=\"language-python\">smallest = None\nprint(&#039;Before:&#039;, smallest)\nfor itervar in [3, 41, 12, 9, 74, 15]:\n    if smallest is None or itervar &lt; smallest:\n        smallest = itervar\n    print(&#039;Loop:&#039;, itervar, smallest)\nprint(&#039;Smallest:&#039;, smallest)<\/code><\/pre>\n<p>Again, <code>smallest<\/code> is the &quot;smallest so far&quot; befor, during, and after the loop executes. When the loop has completed, <code>smallest<\/code> contains the minimum value in the list.<\/p>\n<p>Again as in counting and summing, the build-in functions <code>max()<\/code> and <code>min()<\/code> make writing these exact loops unnecessary.<\/p>\n<p>The following is a simply version of the Python built-in <code>min()<\/code> function:<\/p>\n<pre><code class=\"language-python\">def min(values):\n    smallest = None\n    for value in values:\n        if smallest is None or value &lt; smallest:\n            smallest = value\n    return smallest<\/code><\/pre>\n<p>In the function version of the smallest code, we removed all of the <code>print<\/code> statements so as to be equivalent to the <code>min<\/code> function which is already built in to Python.<\/p>\n<h3>Debugging<\/h3>\n<p>As you start writing bigger programs, you might find yourself spending more time debugging. More code means more chances to make an error and more place for bugs to hide.<\/p>\n<p>One way to cut your debugging time is &quot;debugging by bisection&quot;. For example, if there are 100 lines in you program and you check them one at a time, it works take 100 steps.<\/p>\n<p>Instead, try to break problem in half. Look at the middle of the program, or near it, for an intermediate value you can check. Add a <code>print<\/code> statement (or something else that has a verifiable effect) and run the program.<\/p>\n<p>If the mid-point check is incorrect, the problem must be in the first half of the program. If it is correct, the probem is in the second half.<\/p>\n<p>Every time you perform a check like this, you halve the number of lines you have to search. After six steps(which is much less than 100), you would be down to one or two lines of code, at least in theory.<\/p>\n<p>In practice it is not always clear what the &quot;middle of the problem&quot; is and not always possible to check it. It doesn't make sense to count lines and find the exact midpoint. Instead, think about places in the program where there might be errors and places where it is easy to put a check. Then choose a spot where you think the chances are about the same that the bug is before or after the check.<\/p>\n<h3>Glossary<\/h3>\n<p><strong>accumulator<\/strong><br \/>\nA variable used in a loop to add up or accumulate a result.<\/p>\n<p><strong>counter<\/strong><br \/>\nA variable used in a loop to count the number of times something happened. We initialize a counter to zero and then increment the counter each time we want to &quot;count&quot; something.<\/p>\n<p><strong>decrement<\/strong><br \/>\nAn update that decrease the value of a variable.<\/p>\n<p><strong>initialize<\/strong><br \/>\nAn assignment that gives an initial value to a variable that will be updated.<\/p>\n<p><strong>increment<\/strong><br \/>\nAn update that increase the value of avariable (often by one).<\/p>\n<p><strong>infinite loop<\/strong><br \/>\nA loop in which the terminating condition is never satisfied or for which there is no terminating condition.<\/p>\n<p><strong>iteration<\/strong><br \/>\nRepeated execution of a set of statements using either a function that calls itself or a loop.<\/p>\n<h3>Exercises<\/h3>\n<p><strong>Exercise 1:<\/strong> Write a program which repeatedly reads integers until the user enter &quot;done&quot;. Onec &quot;done&quot; is entered, print out the total, count, and average of the integers. If the user enters anything other than a integers, detect their mistake using <code>try<\/code> and <code>except<\/code> and print an error message and skip to the next integers.<\/p>\n<pre><code class=\"language-python\">total = 0\ncount = 0\nwhile True:\n    inp = input(&#039;Enter a number: &#039;)\n    if inp == &#039;done&#039;:\n        break\n    try:\n        number = int(inp)\n    except:\n        print(&#039;Invalid input&#039;)\n        continue\n    total += number\n    count += 1\n\nprint(total, count, total\/count)<\/code><\/pre>\n<pre><code class=\"language-python\">Enter a number: 4\nEnter a number: 5\nEnter a number: bad data\nInvalid input\nEnter a number: 7\nEnter a number: done\n16 3 5.333333333333333<\/code><\/pre>\n<pre><code class=\"language-python\"># generated by AI code\n# \u521d\u59cb\u5316\u7d2f\u52a0\u5668\u3001\u8ba1\u6570\u5668\uff08\u7b26\u5408\u524d\u6587\u672f\u8bed\u5b9a\u4e49\uff09\ntotal = 0  # \u5b58\u50a8\u6574\u6570\u603b\u548c\uff08\u7d2f\u52a0\u5668\uff09\ncount = 0  # \u7edf\u8ba1\u6709\u6548\u6574\u6570\u4e2a\u6570\uff08\u8ba1\u6570\u5668\uff09\n\nwhile True:\n    # \u8bfb\u53d6\u7528\u6237\u8f93\u5165\uff0c\u53bb\u9664\u9996\u5c3e\u7a7a\u767d\u5b57\u7b26\n    user_input = input(&quot;\u8bf7\u8f93\u5165\u6574\u6570\uff08\u8f93\u5165 &#039;done&#039; \u7ed3\u675f\uff09\uff1a&quot;).strip()\n\n    # \u7ec8\u6b62\u6761\u4ef6\uff1a\u7528\u6237\u8f93\u5165 &#039;done&#039; \u65f6\u9000\u51fa\u5faa\u73af\n    if user_input.lower() == &#039;done&#039;:\n        break\n\n    # \u5c1d\u8bd5\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u6355\u83b7\u975e\u6574\u6570\u8f93\u5165\u7684\u5f02\u5e38\n    try:\n        # \u82e5\u8f6c\u6362\u6210\u529f\uff0c\u66f4\u65b0\u7d2f\u52a0\u5668\u548c\u8ba1\u6570\u5668\n        number = int(user_input)\n        total += number  # \u7d2f\u52a0\u6574\u6570\uff08\u9012\u589e\u64cd\u4f5c\uff09\n        count += 1  # \u8ba1\u6570\u5668\u52a01\uff08\u9012\u589e\u64cd\u4f5c\uff09\n    except ValueError:\n        # \u82e5\u8f6c\u6362\u5931\u8d25\uff0c\u63d0\u793a\u9519\u8bef\u5e76\u8df3\u8fc7\u5f53\u524d\u8f93\u5165\n        print(f&quot;\u9519\u8bef\uff1a\u8f93\u5165 &#039;{user_input}&#039; \u4e0d\u662f\u6709\u6548\u6574\u6570\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)\n        continue  # \u8df3\u8fc7\u540e\u7eed\u903b\u8f91\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\n\n# \u5faa\u73af\u7ed3\u675f\u540e\uff0c\u8ba1\u7b97\u5e76\u6253\u5370\u7ed3\u679c\uff08\u5904\u7406\u9664\u6570\u4e3a0\u7684\u60c5\u51b5\uff09\nif count == 0:\n    print(&quot;\\n\u672a\u8f93\u5165\u4efb\u4f55\u6709\u6548\u6574\u6570\uff01&quot;)\nelse:\n    average = total \/ count  # \u8ba1\u7b97\u5e73\u5747\u503c\uff08\u6d6e\u70b9\u6570\u9664\u6cd5\uff09\n    print(&quot;\\n===== \u7edf\u8ba1\u7ed3\u679c =====&quot;)\n    print(f&quot;\u603b\u548c\uff08Total\uff09\uff1a{total}&quot;)\n    print(f&quot;\u6709\u6548\u6574\u6570\u4e2a\u6570\uff08Count\uff09\uff1a{count}&quot;)\n    print(f&quot;\u5e73\u5747\u503c\uff08Average\uff09\uff1a{average:.2f}&quot;)  # \u4fdd\u75592\u4f4d\u5c0f\u6570\uff0c\u63d0\u5347\u53ef\u8bfb\u6027<\/code><\/pre>\n<p><strong>Exercise 2:<\/strong> Write another program that prompts for a list of numbers as above and at the end prints out both the maximum and minimum of the numbers instead of the average.<\/p>\n<pre><code class=\"language-python\">maximum = None\nminimum = None\nwhile True:\n    inp = input(&#039;Enter a number: &#039;)\n    if inp == &#039;done&#039;:\n        break\n    try:\n        number = int(inp)\n    except:\n        print(&#039;Invalid input&#039;)\n        continue\n    if maximum is None or number &gt; maximum:\n        maximum = number\n    if minimum is None or number &lt; minimum:\n        minimum = number\n\nprint(maximum, minimum)<\/code><\/pre>\n<pre><code class=\"language-python\"># generated by AI code\n# \u521d\u59cb\u5316\u6838\u5fc3\u53d8\u91cf\uff08\u547c\u5e94\u524d\u6587\u672f\u8bed\u4e0e\u903b\u8f91\uff09\ntotal = 0  # \u7d2f\u52a0\u5668\uff1a\u5b58\u50a8\u6709\u6548\u6574\u6570\u603b\u548c\uff08\u53ef\u9009\u4fdd\u7559\uff0c\u9898\u76ee\u672a\u8981\u6c42\u4f46\u4e0d\u5f71\u54cd\u529f\u80fd\uff09\ncount = 0  # \u8ba1\u6570\u5668\uff1a\u7edf\u8ba1\u6709\u6548\u6574\u6570\u4e2a\u6570\nmaximum = None  # \u5b58\u50a8\u6700\u5927\u503c\uff08\u521d\u59cb\u5316\u4e3aNone\uff0c\u6807\u8bb0\u201c\u672a\u8bb0\u5f55\u4efb\u4f55\u503c\u201d\uff09\nminimum = None  # \u5b58\u50a8\u6700\u5c0f\u503c\uff08\u521d\u59cb\u5316\u4e3aNone\uff0c\u6807\u8bb0\u201c\u672a\u8bb0\u5f55\u4efb\u4f55\u503c\u201d\uff09\n\nwhile True:\n    # \u8bfb\u53d6\u7528\u6237\u8f93\u5165\uff0c\u53bb\u9664\u9996\u5c3e\u7a7a\u767d\uff08\u907f\u514d\u7a7a\u683c\u5bfc\u81f4\u7684\u5f02\u5e38\uff09\n    user_input = input(&quot;\u8bf7\u8f93\u5165\u6574\u6570\uff08\u8f93\u5165 &#039;done&#039; \u7ed3\u675f\uff09\uff1a&quot;).strip()\n\n    # \u7ec8\u6b62\u6761\u4ef6\uff1a\u8f93\u5165&#039;done&#039;\uff08\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff09\u65f6\u9000\u51fa\u5faa\u73af\n    if user_input.lower() == &#039;done&#039;:\n        break\n\n    # \u5c1d\u8bd5\u8f6c\u6362\u8f93\u5165\u4e3a\u6574\u6570\uff0c\u6355\u83b7\u975e\u6574\u6570\u5f02\u5e38\n    try:\n        number = int(user_input)\n        # \u66f4\u65b0\u7d2f\u52a0\u5668\u548c\u8ba1\u6570\u5668\uff08\u53ef\u9009\u4fdd\u7559\uff0c\u82e5\u4ec5\u9700\u6700\u503c\u53ef\u5220\u9664\uff09\n        total += number\n        count += 1\n\n        # \u66f4\u65b0\u6700\u5927\u503c\uff1a\u9996\u6b21\u8f93\u5165\u6216\u5f53\u524d\u6570\u5b57\u5927\u4e8e\u73b0\u6709\u6700\u5927\u503c\u65f6\n        if maximum is None or number &gt; maximum:\n            maximum = number\n\n        # \u66f4\u65b0\u6700\u5c0f\u503c\uff1a\u9996\u6b21\u8f93\u5165\u6216\u5f53\u524d\u6570\u5b57\u5c0f\u4e8e\u73b0\u6709\u6700\u5c0f\u503c\u65f6\n        if minimum is None or number &lt; minimum:\n            minimum = number\n\n    except ValueError:\n        # \u975e\u6574\u6570\u8f93\u5165\u65f6\u63d0\u793a\u9519\u8bef\uff0c\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\n        print(f&quot;\u9519\u8bef\uff1a\u8f93\u5165 &#039;{user_input}&#039; \u4e0d\u662f\u6709\u6548\u6574\u6570\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)\n        continue\n\n# \u5faa\u73af\u7ed3\u675f\u540e\uff0c\u6839\u636e\u6709\u6548\u8f93\u5165\u60c5\u51b5\u8f93\u51fa\u7ed3\u679c\nif count == 0:\n    print(&quot;\\n\u672a\u8f93\u5165\u4efb\u4f55\u6709\u6548\u6574\u6570\uff0c\u65e0\u6cd5\u8ba1\u7b97\u6700\u503c\uff01&quot;)\nelse:\n    print(&quot;\\n===== \u7edf\u8ba1\u7ed3\u679c\uff08\u6700\u5927\u503c\/\u6700\u5c0f\u503c\uff09=====&quot;)\n    print(f&quot;\u6709\u6548\u6574\u6570\u4e2a\u6570\uff08Count\uff09\uff1a{count}&quot;)\n    print(f&quot;\u6700\u5927\u503c\uff08Maximum\uff09\uff1a{maximum}&quot;)\n    print(f&quot;\u6700\u5c0f\u503c\uff08Minimum\uff09\uff1a{minimum}&quot;)\n    # \u82e5\u9700\u4fdd\u7559\u603b\u548c\uff0c\u53ef\u6dfb\u52a0\uff1aprint(f&quot;\u603b\u548c\uff08Total\uff09\uff1a{total}&quot;)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Updating variables A common pattern in assignment state&#8230; &raquo; <a class=\"read-more-link\" href=\"http:\/\/blue.yn.cn\/?p=2450\">\u9605\u8bfb\u5168\u6587<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-2450","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/2450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2450"}],"version-history":[{"count":15,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/2450\/revisions"}],"predecessor-version":[{"id":2618,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/2450\/revisions\/2618"}],"wp:attachment":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2450"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}