def lower_to_upper(c1):
if c1>='a' and c1<='z':
c2=chr(ord('A')+ord(c1)-ord('a'))
else:
c2=c1
return c2
lower='g'
upper=lower_to_upper(lower)
print "the upper case equivalent is : ",upper
def lower_to_upper(c1):
if c1>='a' and c1<='z':
return (chr(ord('A')+ord(c1)-ord('a')))
else:
return c1
lower='g'
upper=lower_to_upper(lower)
print "The uppercase equivalent is : ",upper
def maximum(x,y):
if x>=y:
z=x
else:
z=y
print "Maximum value is : ",z
return
maximum(5,6)
maximum(6,5)
maximum(5,5)
def factorial(n):
prod=1
if n>1:
for i in range(2,n+1):
prod=prod*i
return prod
fact=factorial(6)
print "Its factorial is : ",fact
def maximum(x,y):
if x>=y:
z=x
else:
z=y
print 'Maximum value is = ',z
return
maximum(5,9)
maximum(8,3)
def maximum(x,y):
if x>=y:
z=x
else:
z=y
return z
a=5
b=4
c=7
d=maximum(a,b)
print "Maximum = ",maximum(c,d)
def factorial(n):
prod=1
if n>1:
for i in range(2,n+1):
prod=prod*i
return prod
n=7
fact=factorial(n)
print "Its factorial is : ",fact
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(2365)
play()
def modify(a):
a=a*3
print "a= %d (from the function, after being modified)" %(a)
return
a=2
print "a=%d (from main, before calling the function)" %(a)
modify(a)
print "a=%d (from main, after calling the function)" %(a)
def sl(val,n):
deprec=val/n
print "in ",n
for year in range(1,n+1):
val=val-deprec
writeoutput(year,deprec,val)
return
def ddb(val,n):
for year in range(1,n+1):
deprec=2*val/n
val=val-deprec
writeoutput(year,deprec,val)
return
def syd(val,n):
tag=val
for year in range(1,n+1):
deprec=(n-year+1)*tag/(n*(n+1)/2)
val=val-deprec
writeoutput(year,deprec,val)
return
def writeoutput(year,depreciation,value):
print "End of the year %2d Depreciation: %7.2f Current Value: %8.2f" %(year,depreciation,value)
return
def main(choice,val,n):
print "Original value : ",val
val=float(val)
print "Number of years : ",n
if choice==1:
print "Straight-Line Method\n\n"
sl(val,n)
elif choice==2:
print "Double-Declining-Balance Method \n\n"
ddb(val,n)
elif choice==3:
print "Sum-Of-The-Years'-Digits Method"
syd(val,n)
return
print "\n\nMethod: (1-SL 2-DDB 3-SYD)"
main(1,8000,10)
print "\n\nMethod: (1-SL 2-DDB 3-SYD)"
main(2,8000,10)
print "\n\nMethod: (1-SL 2-DDB 3-SYD)"
main(3,8000,10)
print "\n\nMethod: (1-SL 2-DDB 3-SYD)"
main(1,5000,4)
def factorial(n):
if n<=1:
return 1
else:
return (n*factorial(n-1))
n=10
fact=factorial(n)
print "Its factorial is ",fact
def reverse(text,n):
if n<0:
return
else:
print text[n],
reverse(text,n-1)
text='Now is the time for all good men to come to tje aid of their country!'
n=len(text)
reverse(text,n-1)
def transfer(n,From,to,temp):
if n>0:
transfer(n-1,From,temp,to)
print "Move disk %d from %c to %c" %(n,From,to)
transfer(n-1,temp,to,From)
return
print "Welcome to the TOWERS OF HANOI \n\n"
transfer(3,'L','R','C')