# Chapter 8: Program Structure

### Example 8.2, Page number: 8.2

In [1]:
def factorial(n):

prod=1
if n>1:

for i in xrange(2,n+1):
prod*=i

return prod

n=7
print 'n!=%d' %factorial(n)

n!=5040


### Example 8.3, Page number: 8.3

In [2]:
def linecount(text):

return len(text)

Sum=0
lines=['Now is the time for all good men','to come to the aid of their country.']
for i in lines:
Sum+=linecount(i)

count=len(lines)
count=float(count)
avg=Sum/count

print 'Average numbers of characters per line: ',avg


Average numbers of characters per line:  34.0


### Example 8.4, Page number: 8.5

In [3]:
import math

a,b,x1,y1,xr,yr,CNST=0,0,0,0,0,0,0.0001

def reduce():

global x1,xr,y1,yr,a,b

x1 = a+0.5*(b-a-CNST)
xr = x1 + CNST
y1 = curve(x1)
yr = curve(xr)

if y1 > yr:
b = xr

elif y1 < yr:
a = x1

return

def curve(x):

return x*math.cos(x)

a = 0
b = 3.141593

while True:
reduce()
if y1 == yr or b-a <= 3*CNST:
break

xmax = 0.5*(x1+xr)
ymax = curve(xmax)
print 'xmax = %8.6f     ymax = %8.6f' %(xmax,ymax)

xmax = 0.860394     ymax = 0.561096


### Example 8.5, Page number: 8.9

In [4]:
def linecount(text):

global Sum
Sum += len(text)

Sum=0
lines=['Now is the time for all good men','to come to the aid of their country.']
for i in lines:
linecount(i)

count=len(lines)
count=float(count)
avg=Sum/count

print 'Average numbers of characters per line: ',avg


Average numbers of characters per line:  34.0


### Example 8.7, Page number: 8.12

In [5]:
def fibonacci(count):

if count<3:
f=1
else:
f = fibonacci.f1 + fibonacci.f2
fibonacci.f2 = fibonacci.f1
fibonacci.f1 = f

return f

fibonacci.f1,fibonacci.f2=1,1
n=30
for count in range(1,n+1):

print 'i = %2d   F = %d' %(count,fibonacci(count))

i =  1   F = 1
i =  2   F = 1
i =  3   F = 2
i =  4   F = 3
i =  5   F = 5
i =  6   F = 8
i =  7   F = 13
i =  8   F = 21
i =  9   F = 34
i = 10   F = 55
i = 11   F = 89
i = 12   F = 144
i = 13   F = 233
i = 14   F = 377
i = 15   F = 610
i = 16   F = 987
i = 17   F = 1597
i = 18   F = 2584
i = 19   F = 4181
i = 20   F = 6765
i = 21   F = 10946
i = 22   F = 17711
i = 23   F = 28657
i = 24   F = 46368
i = 25   F = 75025
i = 26   F = 121393
i = 27   F = 196418
i = 28   F = 317811
i = 29   F = 514229
i = 30   F = 832040


### Example 8.9, Page number: 8.18

In [4]:
def play():
print "Throwing the dice...."
score1=throw()
print "%2d" %(score1)

if score1==7 or score1==11:
print "Congratulations!! you WIN on the first throw"

elif score1==2 or score1==3 or score1==12:
print "sorry!! you LOSE on the first throw"

else:
while(True):
print "Throwing the dice again..."
score2=throw()
print "%2d" %(score2)
if score2==score1 or score2==7:
break

if score2==score1:
print "You WIN by matching your first score"
else:
print "You LOSE by failing to match your first score"

return

def throw():

n1=random.randrange(1,7)
n2=random.randrange(1,7)
return n1+n2

import random

print "Welcome to the Game of Craps \n\n"
random.seed(563)
play()

Welcome to the Game of Craps

Throwing the dice....
10
Throwing the dice again...
3
Throwing the dice again...
12
Throwing the dice again...
9
Throwing the dice again...
7
You LOSE by failing to match your first score


### Example 8.11, Page Number: 8.21

In [5]:
import math

a,b,x1,y1,xr,yr,CNST=0,0,0,0,0,0,0.0001

def reduce():

global x1,xr,y1,yr,a,b

x1 = a+0.5*(b-a-CNST)
xr = x1 + CNST
y1 = curve(x1)
yr = curve(xr)

if y1 > yr:
b = xr

elif y1 < yr:
a = x1

return

def curve(x):

return x*math.cos(x)

a = 0
b = 3.141593

while True:
reduce()
if y1 == yr or b-a <= 3*CNST:
break

xmax = 0.5*(x1+xr)
ymax = curve(xmax)
print 'xmax = %8.6f     ymax = %8.6f' %(xmax,ymax)

xmax = 0.860394     ymax = 0.561096


### Example 8.12, Page Number: 8.23

In [6]:
def fibonacci(count):

if count<3:
f=1
else:
f = fibonacci.f1 + fibonacci.f2
fibonacci.f2 = fibonacci.f1
fibonacci.f1 = f

return f

fibonacci.f1,fibonacci.f2=1,1
n=40
for count in range(1,n+1):

print 'i = %2d   F = %d' %(count,fibonacci(count))

i =  1   F = 1
i =  2   F = 1
i =  3   F = 2
i =  4   F = 3
i =  5   F = 5
i =  6   F = 8
i =  7   F = 13
i =  8   F = 21
i =  9   F = 34
i = 10   F = 55
i = 11   F = 89
i = 12   F = 144
i = 13   F = 233
i = 14   F = 377
i = 15   F = 610
i = 16   F = 987
i = 17   F = 1597
i = 18   F = 2584
i = 19   F = 4181
i = 20   F = 6765
i = 21   F = 10946
i = 22   F = 17711
i = 23   F = 28657
i = 24   F = 46368
i = 25   F = 75025
i = 26   F = 121393
i = 27   F = 196418
i = 28   F = 317811
i = 29   F = 514229
i = 30   F = 832040
i = 31   F = 1346269
i = 32   F = 2178309
i = 33   F = 3524578
i = 34   F = 5702887
i = 35   F = 9227465
i = 36   F = 14930352
i = 37   F = 24157817
i = 38   F = 39088169
i = 39   F = 63245986
i = 40   F = 102334155


### Example 8.13, Page Number: 8.25

In [7]:
import math

p,r,n=10000,10,3
i=r/100.0

f=p*math.pow(1+i,n)
print "The final value (F) is: %.2f" %f

The final value (F) is: 13310.00

In [ ]: