Chapter 7 - The For Loop

example 7.1, page no. 142

In [1]:
num = 2
num += 1
print num
3

example 7.2, page no. 142

In [2]:
num = 2
num += 1
print num
3

example 7.3, page no. 142

In [3]:
num = 2
print num
num += 1
2

example 7.4, page no. 143

In [4]:
num = 2
num -= 1
print num
1

example 7.5, page no. 143

In [1]:
num = 2
num -= 1
print num
1

example 7.6, page no. 144

In [3]:
num = 2
num -= 1
print num
1

example 7.7, page no. 146

In [9]:
num = 1
print num
num += 1
print num
num += 1
print num
num += 1
print num
num += 1
print num
num += 1
print num
num += 1
print num
num += 1
print num
num += 1
print num
num += 1
print num
1
2
3
4
5
6
7
8
9
10

example 7.8, page no. 146

In [10]:
for num in range(1, 11):
    print num
1
2
3
4
5
6
7
8
9
10

example 7.9, page no. 147

In [12]:
for num in range(1, 101):
    print num,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

example 7.10, page no. 149

In [14]:
num = 1
while num <= 10:
    print num,
    num += 1
1 2 3 4 5 6 7 8 9 10

example 7.11, page no. 149

In [ ]:
num = 1
while num <= 10:
    print num,

example 7.12, page no. 150

In [16]:
total = 1;
print "Enter a number: ",
num = int(raw_input())
print "The factorial of ", num, " is ",
for counter in range(1, num+1):
    total *= counter
print total
Enter a number: 5
 The factorial of  5  is  120

example 7.13, page no. 150

In [17]:
secret = 3
print "Guess a number between 1 and 10"
print "You have 3 tries"
for counter in range(1,4):
    print "Enter the number now: ",
    num = int(raw_input())
    if (num == secret):
        print "You guessed the secret number!"
        break
print "Program over"
Guess a number between 1 and 10
You have 3 tries
Enter the number now: 2
 Enter the number now: 5
 Enter the number now: 4
 Program over

example 7.14, page no. 151

In [18]:
secret = 3
print "Guess a number between 1 and 10\n"
print "You have 3 tries\n"
keepgoing = True
for counter in range(1, 4):
    if keepgoing == True:
        print "Enter the number now: ",
        num = int(raw_input())
        if (num == secret):
            print "You guessed the secret number!"
            keepgoing = False
    else:
        break
print "Program over"
Guess a number between 1 and 10

You have 3 tries

Enter the number now: 3
 You guessed the secret number!
Program over

example 7.15, page no. 152

In [20]:
num = 1
while True:
    if (num > 10):
        break
    else:
        print num, " ",
        num += 1
1   2   3   4   5   6   7   8   9   10  

example 7.16, page no 153

In [21]:
total = 0
print "How many items do you want to buy: ",
num = int(raw_input())
for counter in range(1, num+1):
    if (counter % 13 == 0):
        continue
    total += 3
print "Total for ", num, " items is $", total
How many items do you want to buy: 5
 Total for  5  items is $ 15

example 7.17, page no. 154

In [22]:
total = 0
print "How many items do you want to buy: ",
num = int(raw_input())
keepgoing = True
for counter in range(1, num+1):
    if (not(counter % 13 == 0 )):
        total += 3
print "Total for ", num, " items is $", total
How many items do you want to buy: 6
 Total for  6  items is $ 18

example 7.18, page no. 154

In [23]:
for x in range(1, 6):
    for y in range(1, 11):
        print "X",
    print "\n"
X X X X X X X X X X 

X X X X X X X X X X 

X X X X X X X X X X 

X X X X X X X X X X 

X X X X X X X X X X 

example 7.19, page no. 155

In [24]:
print "Enter number of salespersons: ",
persons = int(raw_input())
print "Enter number of sales per salesperson: ",
numSales = int(raw_input())
for x in range(1, persons+1):
    total = 0
    for y in range(1, numSales+1):
        print "Enter sale ", y, " for salesperson ", x, ": "
        sale = int(raw_input())
        total += sale
    average = float(total/numSales)
    print "Average sales for salesperson #", x, " is ", average
Enter number of salespersons: 3
 Enter number of sales per salesperson: 2
 Enter sale  1  for salesperson  1 : 
1
Enter sale  2  for salesperson  1 : 
2
Average sales for salesperson # 1  is  1.0
Enter sale  1  for salesperson  2 : 
3
Enter sale  2  for salesperson  2 : 
4
Average sales for salesperson # 2  is  3.0
Enter sale  1  for salesperson  3 : 
5
Enter sale  2  for salesperson  3 : 
6
Average sales for salesperson # 3  is  5.0