thailandrefa.blogg.se

Python while loop
Python while loop













python while loop

If you copy in and use the function isDecimalStr.

python while loop

If you copy in and use the function (not method) isIntegerStr. Where you are trying to enter a whole number.Ĭomplete the definition of the function safeWholeNumber. It contains headings and documentation stringsįor the functions in each part of this exercise. Save the example safeNumberInputStub.py as safeNumberInput.py, refer to functions in the solution, isNumberStr.py, of In this exercise write safe utility function replacementsįor the input function that work to read in a whole number, an integer or This is avoidable if you test the string and repeat if the string is illegal. String to the right kind of number, a naive program will bomb. If you make a typo and enter something that cannot be converted from a * There is an issue with reading in numbers with the input statement. You actually can stop the program by entering Ctrl-C. Variable line will forever have the initial value you gave it! It is easy to forget the second placeĪfter reading the rest of this paragraph,Ĭomment the last line of the loop out, and run it again: Statements setting the variable line both before the loop andĪt the end of the loop body. Loop for the test after the body has executed.

python while loop

Initialized before the first time the while statement isĮxecuted and the test data must also be made ready inside the append ( line ) line = input ( 'Next line: ' ) # !! reset value at end of loop! print ( 'Your lines were:' ) for line in lines : print ( line )Īgain the data for the test in the while loop heading must be Lines = list () print ( 'Enter lines of text.' ) print ( 'Enter an empty line to quit.' ) line = input ( 'Next line: ' ) # initalize before the loop while line != '' : # while NOT the termination condition lines. Termination condition, not the continuation condition: You need What should the while condition be now? Since the sentinel is anĮmpty line, you might think line = '', but that is the Simple) line, no matter how many lines of real data you have. Indented body.) This way you only need to enter one extra (very Shell uses this approach when you enter a statement with an Real lines of data will actually have some text on them, use anĮmpty line as a sentinel. You couldĪgree to use the line DONE! Even simpler: if you assume all the Piece of data that would not make sense in the regular sequence,Īnd which is used to indicate the end of the input.

#PYTHON WHILE LOOP CODE#

The readLines1.py code works, but it may be more annoying thanĬounting ahead! Two lines must be entered for every one youĪctually want! A practical alternative is to use a sentinel: a This means theĭata for the test must also be set up a second time, in the loopīody (commonly as the action in the last line of the loop). When you loop back from the end of the loop body. The data must be initialized before the loop, in order for theįirst test of the while condition to work.

python while loop

Modification loop introduced with for-each loops: The while loop to the next sequential statement.Ī while loop generally follows the pattern of the successive Test is finally false, execution jumps past the indented body of Returns to the while loop heading for another test. The end of the indented block after the while heading,Įxecution returns to the while heading for another test: LineĮach time the end of the indented loop body is reached, execution If you play computer and follow the path of execution, you could I added a final line after the while loop to remind you thatĮxecution follows sequentially after a loop completes. Temperature = 115 while temperature > 112 : # first while loop code print ( temperature ) temperature = temperature - 1 print ( 'The tea is cool enough.' )















Python while loop