Document

While Loops in Python
1
A BIT UNFINISHED BUSINESS WITH
FOR LOOPS …
2
Two kinds of for loops
Element-based Loops
Index-based Loops
sum = 0
sum = 0
for x in aList:
sum += x
for i in
sum +=
:
i
aList = [ 42, -5, 10 ]
1
2
0
aList = [ 42, -5, 10 ]
x
3
Two kinds of for loops
Element-based Loops
Index-based Loops
sum = 0
sum = 0
for x in aList:
sum += x
for i in range(len(aList)):
sum += aList[i]
i
aList = [ 42, -5, 10 ]
x
1
2
0
aList = [ 42, -5, 10 ]
aList[i]
4
Nested loops
for row in range(3):
print('# # # # ')
output?
5
Nested loops
for row in range(3):
print('# # # # ')
# # # #
# # # #
# # # #
Not particularly flexible!
6
Nested loops
for row in range(3):
for col in range(4):
print('#')
# # # #
# # # #
# # # #
Is this still the
output?
NO! What changes
are needed?
Nested loops are powerful – and flexible...
7
Tracking rows and columns
for row in range(3):
for col in range(4):
print('$', end='')
print()
0
1
2
3
cols
0
1
2
rows
8
Try these examples
for row in range( 3 ):
for col in range( 6 ):
print(_________)
print()
cols
0
0
1
1
2
3
4
5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
2
rows
Change each block of code so that it will print the examples below:
for row in range( 3 ):
for col in range( 6 ):
print(____________)
print()
0 0 0 0 0 0
1 1 1 1 1 1
2 2 2 2 2 2
for row in range( 3 ):
for col in range( 6 ):
print(____________)
print()
0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
for row in range( 3 ):
for col in range( 6 ):
print(____________)
print()
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
Python ‘for’ loop works well for a collection of
known items. That is, we know the count, or we
know the list of items.
But what if we don’t know the count, or the
list, rather we only know the condition to
repeat (or not to repeat) the steps? For
example, we want to repeat adding to a sum if
the user input is a positive number.
10
Here is a problem to solve:
The user types in a sequence of positive integers. The
program is to add up the integers. The user signals
the end of the input by entering a non-positive value
which should not be part of the sum.
sum = 0
v = int( input( ‘Enter a positive integer ( <= 0 to stop) : ‘))
while ( v > 0):
sum = sum + v
v = int( input( ‘Enter a positive integer ( <= 0 to stop) : ‘))
print( ‘Summation is : ‘, sum )
11
while <condition>:
<body>
x = 10
if x != 0:
print(x)
x = x – 1
print(x)
while loops:
indefinite, conditional
iteration
What are printed
on the screen?
12
while <condition>:
<body>
x = 10
while x != 0:
print(x)
x = x – 1
print(x)
while loops:
indefinite, conditional
iteration
What is the last value
printed on the screen?
0
1
9
10
13
Patterns of while loop
Prime the condition.
while <condition>:
<body>
<update condition>
x = 10
while x != 0:
print(x)
x = x – 1
print(x)
Check the condition.
Update the condition.
From logical point of view,
while loop has three
components.
1. Prime the condition;
2. Check the condition;
3. Update the condition.
14
Extreme Looping
What does this code do?
print('It keeps on')
while True:
print('going and')
print('Phew! I\'m done!')
15
Making our escape!
import random
escape = 0
Will the same thing appear
every time this is run?
while escape != 42:
print('Help! Let me out!')
escape = random.choice([41,42,43])
print('At last!')
17
Count the iterations…
import random
escape = 0
What modifications do we need to
make to count the number of
iterations and report it?
while escape != 42:
print('Help! Let me out!')
escape = random.choice([41,42,43])
print('At last!')
18
how could we make it easier/harder to escape?
Count the iterations…
import random
escape = 0
count = 0
What modifications do we need to
make to count the number of
iterations and report it?
while escape != 42:
count += 1
print('Help! Let me out!')
escape = random.choice([41,42,43])
print('At last!')
19
how could we make it easier/harder to escape?