# Chapter 4: Loops¶

## Program 4.1, page no. 139¶

In :
for count in range (1, 11):
print count,
print "\nAfter the loop count has the value %d." %count

1 2 3 4 5 6 7 8 9 10
After the loop count has the value 10.


## Program 4.2, page no. 140¶

In :
print "**************"
for count in range(1, 9):
print "*            *"
print "**************"

**************
*            *
*            *
*            *
*            *
*            *
*            *
*            *
*            *
**************


## Program 4.3, page no. 144¶

In :
sum = 0

print "Enter the number of integers you want to sum: ",
count = int(raw_input())

for i in range(1, count+1):
sum = sum + i

print "Total of the first %d numbers is %d" %(count, sum)

Enter the number of integers you want to sum: 10
Total of the first 10 numbers is 55


## Program 4.4, page no. 144¶

In :
sum = 0

print "Enter the number of integers you want to sum: ",
count = int(raw_input())

for i in range(1, count+1):
sum = sum + i

print "Total of the first %d numbers is %d" %(count, sum)

Enter the number of integers you want to sum: 10
Total of the first 10 numbers is 55


## Program 4.5, page no. 147¶

In :
sum = 0

print "Enter the number of integers you want to sum: ",
count = int(raw_input())
for i in range(count, 0, -1):
sum = sum + i
print "Total of the first %d numbers is %d" %(count, sum)

Enter the number of integers you want to sum: 10
Total of the first 10 numbers is 55


## Program 4.6, page no. 148¶

In :
print "This program calculates the average of any number of values."
total = 0
count = 0
while True: # infinite loop
print "\nEnter a value: ",
value = float(raw_input())
total += value
count += 1

print "Do you want to enter another value? (Y or N): ",
answer = raw_input()
if(answer.lower() == 'n'):
break

print "The average is %.2f" %(total/count)

This program calculates the average of any number of values.

Enter a value: 10
Do you want to enter another value? (Y or N): y

Enter a value: 8
Do you want to enter another value? (Y or N): y

Enter a value: 6
Do you want to enter another value? (Y or N): n
The average is 8.00


## Program 4.7, page no. 151¶

In :
import sys

chosen = 15
guess = 0
count = 3

print "This is a guessing game."
print "I have chosen a number between 1 and 20 which you must guess."

for i in range(count, 0, -1):
print "You have %d tr%s left." %(i, ("y" if i == 1 else "ies"));
print "\nEnter a guess: ",
guess = int(raw_input())
if(guess == chosen):
print "Congratulations. You guessed it!"
sys.exit()
elif(guess < 1 or guess > 20):
print "I said the number is between 1 and 20."
else:
print "Sorry, %d is wrong. My number is %s than that.\n" %(guess, ("greater" if chosen > guess else "less"))
print "You have had three tries and failed. The number was %d" % chosen

This is a guessing game.
I have chosen a number between 1 and 20 which you must guess.
You have 3 tries left.

Enter a guess: 5
Sorry, 5 is wrong. My number is greater than that.

You have 2 tries left.

Enter a guess: 9
Sorry, 9 is wrong. My number is greater than that.

You have 1 try left.

Enter a guess: 14
Sorry, 14 is wrong. My number is greater than that.

You have had three tries and failed. The number was 15


## Program 4.7A, page no. 154¶

In :
from random import *

guess = 0
count = 3
limit = 20

chosen = 1 + randint(1, limit) #Random int 1 to limit

print "This is a guessing game."
print "I have chosen a number between 1 and 20 which you must guess."

for i in range(count, 0, -1):
print "You have %d tr%s left." %(i, ("y" if i == 1 else "ies"));
print "Enter a guess: ",
guess = int(raw_input())
if(guess == chosen):
print "Congratulations. You guessed it!"
sys.exit()
elif(guess < 1 or guess > 20):
print "I said the number is between 1 and 20."
else:
print "Sorry, %d is wrong. My number is %s than that.\n" %(guess, ("greater" if chosen > guess else "less"))
print "You have had three tries and failed. The number was %d" % chosen

This is a guessing game.
I have chosen a number between 1 and 20 which you must guess.
You have 3 tries left.
Enter a guess: 6
Sorry, 6 is wrong. My number is greater than that.

You have 2 tries left.
Enter a guess: 14
Sorry, 14 is wrong. My number is greater than that.

You have 1 try left.
Enter a guess: 16
Sorry, 16 is wrong. My number is greater than that.

You have had three tries and failed. The number was 20


## Program 4.8, page no. 158¶

In :
sum = 0
i = 1

print "Enter the number of integers you want to sum: ",
count = int(raw_input())

while(i <= count):
sum += i
i += 1

print "Total of the first %d numbers is %d" %(count, sum)

Enter the number of integers you want to sum: 10
Total of the first 10 numbers is 55


## Program 4.9, page no. 161¶

In :
MIN_SIZE = 3;

print "Enter values for the width and height (minimum of %d): " % MIN_SIZE
width = int(raw_input("Width: "))
height = int(raw_input("Height: "))

if(width < MIN_SIZE):
print "Width value of %d is too small. Setting it to %d." %(width, MIN_SIZE)
width = MIN_SIZE
if(height < MIN_SIZE):
print "Height value of %d is too small. Setting it to %d." %(height, MIN_SIZE)
height = MIN_SIZE

for i in range(0, width):
print "*",
print "\n"
for j in range(0, height-2):
print "*",
for i in range(0, width-2):
print " ",
print "*"

print "\n"
for i in range(0, width):
print "*",

Enter values for the width and height (minimum of 3):
Width: 24
Height: 7
* * * * * * * * * * * * * * * * * * * * * * * *

*                                             *
*                                             *
*                                             *
*                                             *
*                                             *

* * * * * * * * * * * * * * * * * * * * * * * *


## Program 4.10, page no. 162¶

In :
sum = 0
count = 0

print "Enter the number of integers you want to sum: ",
count = int(raw_input())

for i in range(1, count+1):
sum = 0
for j in range(1, i+1):
sum += j
print "%d\t%5d" %(i, sum)
print "\n"

Enter the number of integers you want to sum: 10
1	    1
2	    3
3	    6
4	   10
5	   15
6	   21
7	   28
8	   36
9	   45
10	   55



## Program 4.11, page no. 164¶

In :
print "Enter the number of integers you want to sum: ",
count = int(raw_input())

for i in range(1, count+1):
sum = 1
j = 1
print "1",
while(j < i):
j += 1
sum += j
print "+ %d" % j,
print "= %d" % sum
print "\n"

Enter the number of integers you want to sum: 10
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15
1 + 2 + 3 + 4 + 5 + 6 = 21
1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55



## Program 4.12, page no. 167¶

In :
rebmun = 0
print "\nEnter a positive integer: ",
number = int(raw_input())
temp = number
while(temp):
rebmun = 10*rebmun + temp % 10
temp = temp/10

print "The number %d reversed is %d rebmun ehT" %(number, rebmun)

Enter a positive integer: 7
The number 7 reversed is 7 rebmun ehT