For example, in this script:
The while loop tells Python to keep prompting for input as long as some string other than '' is entered.#!/usr/bin/python3print ('List Accumulator')
samplelist = []
print ('Please enter items for list below. When list is finished, just enter nothing instead of item.')
item = 'x'
while item != '':
item = input('Next item? ')
if item != '':
samplelist.append(item)
print ('Currently', len(samplelist), 'items in list')
print ('Complete list is:')
print (samplelist)
The output is as follows:
RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test05_whileloop.pyApart from the while loop, there is nothing particularly new in this script.
List Accumulator
Please enter items for list below. When list is finished, just enter nothing instead of item.
Next item? Hello
Currently 1 items in list
Next item? World
Currently 2 items in list
Next item?
Currently 2 items in list
Complete list is:
['Hello', 'World']
>>>
It's the first time I've used the != (not equal to) comparison on this blog.
item = 'x' is a necessary workaround to initialise the item variable - Python does not like trying to compare something it does not have a value for. 'x' is discarded with the first input.
The if structure inside the while loop is there to prevent '' being the last item in the finished list.
A while loop can be used instead of a for loop.
gives the output:#!/usr/bin/python3x = 0while x < 10:
print (x, 'Hello World')
x += 1
RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test05_simpleWhile.py
0 Hello World
1 Hello World
2 Hello World
3 Hello World
4 Hello World
5 Hello World
6 Hello World
7 Hello World
8 Hello World
9 Hello World
>>>
Trapped in an Infinite Loop?
while loops can create a situation where the program and user cannot break out of the loop - it just goes round and round. This is a form of logical error - the computer is just doing as it is told.The way to escape this is ctrl-c on the keyboard (ctrl key + c key at same time). This will break out of many (most?) running programs.
This script demonstrates:
However, I have found that Ctrl-C is not always reliable (maybe it tries to interrupt at the wrong point in the script?), and that it may be necessary (or at least quicker) to close the Python/IDLE window. Generally it's best not to get into that situation in the first place. Maybe I should have mentioned that before offering the above script...#!/usr/bin/python3x = 1
while (x):
print ('Infinite loop! Ctrl-C to escape!')
break
Generally speaking, when you want to finish a while loop, you include one or more conditions in the while statement at the top of the loop, so it stops looping when the condition is no longer true. Similarly with a for loop, you expect it to run for however many iterations you state at the start or until it reaches the end of a list or other collection. However, there may be situations when you want to stop the loop and continue with the rest of the program. This is where break comes in. It allows the program to break out of the loop prematurely. Here's a script that uses break.So if I run it and enter a high number:#!/usr/bin/python3b = input("Please input number of repetitions:")
b = int(b)
for a in range(b):
print (a, " Hello World!")
if a >= 50:
print ('Sorry, this is getting tedious.')
break
RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test05_for_break.py
Please input number of repetitions:56
0 Hello World!
1 Hello World!
2 Hello World!
......
47 Hello World!
48 Hello World!
49 Hello World!
50 Hello World!
Sorry, this is getting tedious.
>>>
You could conceivably use break as a check against infinite loops, such as in the script above. It would be up to the programmer to decide when a loop is not serving any purpose and has become a nuisance, and thus create an if statement and break to catch this.
No comments:
Post a Comment