![]() ![]() ![]() It reaches its termination condition, (i.e. The condition for continuing with this loop is n != 1, so the loop will continue running until (You’ll cover the \n (newline character) in the next chapter). Will then print the final value of n followed by a period and a newline character. The call to print(n, end=".\n") at line 11 after the loop terminates SoĮach time something is printed in the loop, it is printed on the same output line, with Tells the print function to follow the printed string with whatever the programmerĬhooses (in this case, a comma followed by a space), instead of ending the line. Notice first that the print function on line 6 has an extra argument end=", ". """ while n != 1 : print ( n, end = ", " ) if n % 2 = 0 : # n is even n = n // 2 else : # n is odd n = n * 3 + 1 print ( n, end = ". By comparison, here is an equivalent function that uses for instead:ĭef seq3np1 ( n ): """ Print the 3n+1 sequence from n, terminating when it reaches 1. Loop one has to manage the loop variable yourself: give it an initial value, testįor completion, and then make sure you change something in the body so that the loop You - the programmer - than the equivalent for loop. What you will notice here is that the while loop is more work for Other cases, it is not so easy, even impossible in some cases, Increments each time through the loop, so eventually it will have to exceed n. Know that the value of n is finite, and we can see that the value of v In the case here, we can prove that the loop terminates because we Source of amusement for computer scientists is the observation that theĭirections on shampoo, “lather, rinse, repeat”, are an infinite loop. Loop will repeat forever, which is called an infinite loop. The body of the loop should change the value of one or more variables so thatĮventually the condition becomes false and the loop terminates. Loop, the statements in the body of the loop are never executed. Notice that if the loop condition is False the first time we get The body consists of all of the statements indented below the while keyword. Then go back to the while statement at line 5. If the value is True, execute each of the statements in the body (lines 6 and 7) and.If the value is False, exit the while statement and continueĮxecution at the next statement (line 8 in this case).Evaluate the condition at line 5, yielding a value which is either False or True.More formally, here is precise flow of execution for a while statement: When v passes n, return your accumulated sum. ![]() While v is less than or equal to n, continue executing the body of the loop. You can almost read the while statement as if it were English. n """ ss = 0 v = 1 while v <= n : ss = ss + v v = v + 1 return ss # For your test suite test ( sum_to ( 4 ) = 10 ) test ( sum_to ( 1000 ) = 500500 ) Def sum_to ( n ): """ Return the sum of 1+2+3. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |