CHAPTER 2: THE DECISION CONTROL STRUCTURE

EXAMPLE ON PAGE-45

In [1]:
 
print "Enter a number less than 10"
num=eval(raw_input())                                #Inputs float variables #you can also input float variables using input()
if num<10:
    print "What an obedient servant you are !\n"
Enter a number less than 10
5
What an obedient servant you are !

Example 2.1: Page-47

In [2]:
 
print "Enter quantity and rate"
qty=eval(raw_input())                               #Inputs float variables
rate=eval(raw_input())
dis=0
if qty>1000:
    dis=10
tot=(qty*rate)-(qty*rate*dis/100)
print "Total expenses=Rs." ,tot
Enter quantity and rate
1200
15.50
Total expenses=Rs. 16740.0

Example 2.2: Page-49

In [4]:
 
print "Enter current year and year of joining"
cy=eval(raw_input())
yoj=eval(raw_input())
yos=cy-yoj
if yos>3:
    bonus=2500
    print "Bonus=Rs." ,bonus
Enter current year and year of joining
2014
2009
Bonus=Rs. 2500

Example 2.3: Page-51

In [5]:
 
print "Enter basic salary"
bs=eval(raw_input())
if bs<1500:
    hra=bs*10/100
    da=bs*90/100
else:
    hra=500
    da=bs*98/100
gs=bs+hra+da
print "gross salary=Rs." ,gs
Enter basic salary
1000
gross salary=Rs. 2000

EXAMPLE ON Page-53

In [6]:
 
print "Enter either 1 or 2"
i=eval(raw_input())
if i==1:
    print "You would go to heaven !\n"
else:
    if i==2:
        print "Hell was created with you in mind\n"
    else:
        print "How about mother earth !\n"
Enter either 1 or 2
3
How about mother earth !

Example 2.4: Page-55-56

In [7]:
 
print "Enter marks in five subjects"
m1=eval(raw_input())
m2=eval(raw_input())
m3=eval(raw_input())
m4=eval(raw_input())
m5=eval(raw_input())
per=(m1+m2+m3+m4+m5)*100/500
if per>=60:
    print "First division\n"
else:
    if per>=50:
        print "Second division\n"
    else:
        if per>=40:
            print "Third division\n"
        else:
            print "Fail\n"
Enter marks in five subjects
70
87
91
91
98
First division

EXAMPLE ON Page-56-57

In [8]:
 
print "Enter marks in five subjects"
m1=eval(raw_input())
m2=eval(raw_input())
m3=eval(raw_input())
m4=eval(raw_input())
m5=eval(raw_input())
per=(m1+m2+m3+m4+m5)*100/500
if per>=60:
    print "First division\n"
if per>=50 and per<60:
    print "Second division\n"
if per>=40 and per<50:
    print "Third division\n"
if per<40:
    print "Fail\n"
Enter marks in five subjects
70
87
91
91
98
First division

EXAMPLE ON Page-58

In [9]:
 
print "Enter marks in five subjects"
m1=eval(raw_input())
m2=eval(raw_input())
m3=eval(raw_input())
m4=eval(raw_input())
m5=eval(raw_input())
per=(m1+m2+m3+m4+m5)*100/500
if per>=60:
    print "First division\n"
elif per>=50:
    print "Second division\n"
elif per>=40:
    print "Third division\n"
else:
    print "fail\n"
Enter marks in five subjects
70
87
91
91
98
First division

Example 2.5: Page-59

In [10]:
 
print "Enter age,sex,marital status"
age=eval(raw_input())
sex=raw_input()                                         #Inputs string variables i,e; we can't use these variables in calculations
ms=raw_input()                                          #Inputs string variables
if ms=='M':
    print "Driver is insured\n"
else:
    if sex=='M':
        if age>30:
            print "Driver is insured\n"
        else:
            print "Driver is not insured\n"
    else:
        if age>25:
            print "Driver is insured\n"
        else:
            print "Driver is not insured\n"
Enter age,sex,marital status
20
M
U
Driver is not insured

EXAMPLE ON Page-60

In [11]:
 
print "Enter age,sex,marital status"
age=eval(raw_input())
sex=raw_input()
ms=raw_input()
if (ms=='M') or (ms=='U' and sex=='M' and age>30) or (ms=='U' and sex=='F' and age>25):
    print "Driver is insured\n"
else:
    print "Driver is not insured\n"
Enter age,sex,marital status
20
M
U
Driver is not insured

Example 2.6: Page-61-62

In [12]:
 
print "Enter Gender,Years of Service and Qualifications(0=G,1=PG):"
g=raw_input()
yos=eval(raw_input())
qual=eval(raw_input())
sal=0
if g=='m' and yos>=10 and qual==1:
    sal=15000
elif (g=='m' and yos>=10 and qual==0) or (g=='m' and yos<10 and qual==1):
    sal=10000
elif g=='m' and yos<10 and qual==0:
    sal=7000
elif g=='f' and yos>=10 and qual==1:
    sal=12000
elif g=='f' and yos>=10 and qual==0:
    sal=9000
elif g=='f' and yos<10 and qual==1:
    sal=10000
elif g=='f' and yos<10 and qual==0:
    sal=6000
print "\nSalary of Employee=" ,sal
Enter Gender,Years of Service and Qualifications(0=G,1=PG):
m
5
0

Salary of Employee= 7000