Chapter 2: The Decision Control Structure

If Demo , Page number: 52

In [1]:
print "Enter a number less than 10: "
num = 8
print num

#if statement
if num <= 10:
    print("What an obedient servant you are !") #display result
Enter a number less than 10: 
8
What an obedient servant you are !

Example 2.1 , Page number: 53

In [2]:
dis = 0 #Initial Discount (%0)

#Input from the user
#qty,rate = raw_input("Enter quantity and rate: ").split()
print "Enter quantity and rate: "
qty = 1200  # Quantity of item
rate = 15.50 # Rate of item (Rs)
print qty , rate

#discount of 10% if quantity > 1000
if qty > 1000:
    dis = 10

#Calculation
tot = (qty * rate) - (qty * rate * dis / 100 )  # total expenses (Rs)

#Result
print "Total expenses = Rs. ", tot 
Enter quantity and rate: 
1200 15.5
Total expenses = Rs.  16740.0

Example 2.2, Page number: 57

In [3]:
#cy,yoj = raw_input("Enter current year and year of joining: ").split() 
print "Enter current year and year of joining: "
cy = 2013 # Current year
yoj = 1990 # Year of joining
print cy, yoj 
#Calculation
yr_of_ser = cy - yoj # number of years of service

#Assign bonus if years of service > 3
if yr_of_ser > 3:
    bonus = 2500 # Bonus of Rs. 2500
    print "Bonus = Rs.", bonus  #display result
Enter current year and year of joining: 
2013 1990
Bonus = Rs. 2500

Example 2.3 , Page number: 58

In [4]:
print "Enter basic salary: "
bs = 2561.1 #Basic salary (Rs)
print bs

#Calculation
if bs < 1500: # if basic salary is less than Rs.1500
    hra = bs * 10 / 100 # HRA (Rs)
    da = bs * 90 / 100 #DA (Rs)
else: #if basic salary is greater than or equal to Rs.1500
    hra = 500  # HRA (Rs)
    da = bs * 98 / 100 # DA (Rs)

gs = bs + hra + da # gross salary (Rs)

#Result
print "gross salary = Rs. ", gs 
Enter basic salary: 
2561.1
gross salary = Rs.  5570.978

Nested If-else , Page number: 61

In [5]:
print "Enter either 1 or 2: "
i = 1
print i

#nested if-else
if i == 1 :
    print "You would go to heaven !" 
else:
    if i == 2 :
        print "Hell was created with you in mind" 
    else:
        print  "How about mother earth !" 
Enter either 1 or 2: 
1
You would go to heaven !

Example 2.4 (Method 1), Page number: 64

In [7]:
print "Enter marks in five subjects: "
m1 = 88 #Marks in 1st subject
m2 = 92 #Marks in 2nd subject
m3 = 87 #Marks in 3rd subject
m4 = 66 #Marks in 4th subject
m5 = 56 #Marks in 5th subject
print m1,m2,m3,m4,m5

#Calculation
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage

#check for different cases and display appropriate result
if per >= 60:
    print "First division"
else:
    if per >= 50:
        print "Second division"
    else:
        if per >= 40:
            print "Third division"
        else:
            print "Fail"
 Enter marks in five subjects: 
88 92 87 66 56
First division

Example 2.4 (Method 2), Page number: 65

In [8]:
print "Enter marks in five subjects: "
m1 = 88 #Marks in 1st subject
m2 = 92 #Marks in 2nd subject
m3 = 87 #Marks in 3rd subject
m4 = 66 #Marks in 4th subject
m5 = 56 #Marks in 5th subject
print m1,m2,m3,m4,m5

#Calculation
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage

#check for different cases and display appropriate result
if per >= 60:
    print "First division"

if (per >= 50) and (per <60):
    print"Second division"

if (per >= 40) and (per <50):
    print"Third division"

if per < 40 :
    print"Fail"
Enter marks in five subjects: 
88 92 87 66 56
First division

Example 2.4 (Method 3), Page number: 67

In [9]:
print "Enter marks in five subjects: "
m1 = 88 #Marks in 1st subject
m2 = 92 #Marks in 2nd subject
m3 = 87 #Marks in 3rd subject
m4 = 66 #Marks in 4th subject
m5 = 56 #Marks in 5th subject
print m1,m2,m3,m4,m5

#Calculation
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage

#check for different cases and display appropriate result
if per >= 60:
    print"First division"
elif per >= 50:
    print"Second division"
elif per >= 40:
    print"Third division"
else:
    print"Fail"
Enter marks in five subjects: 
88 92 87 66 56
First division

Example 2.5 (Method 1) , Page number: 68

In [11]:
print "Enter age, sex, marital status: "
age = 43 # Age of driver (years)
sex = 'M'
ms = 'M'
print age,sex,ms
#check for different cases and display appropriate result
if ms == 'M':
    print("Driver is insured")
else:
    if sex == 'M':
        if age > 30:
            print ("Driver is insured")
        else:
            print ("Driver is not insured")
    else:
        if age > 25:
            print ("Driver is insured")
        else:
            print ("Driver is not insured")
                
Enter age, sex, marital status: 
43 M M
Driver is insured

Example 2.5 (Method 2) , Page number: 69

In [12]:
print "Enter age, sex, marital status: "
age = 43 # Age of driver (years)
sex = 'M'
ms = 'M'
print age,sex,ms

#check for different cases and display appropriate result
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"
else:
    print"Driver is not insured"
Enter age, sex, marital status: 
43 M M
Driver is insured

Example 2.6, Page number: 71

In [13]:
print "Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):"
g = 'f'
yos = 8 # Years of service(years)
qual = 1 # Qualification ( 0=G, 1=PG)
print g,yos,qual

# Assign salary depending upon the conditions
if (g == 'm') and (yos >= 10) and (qual == 1):
    sal = 15000 #salary
elif ((g == 'm' and yos >= 10 and qual == 0) or ( g == 'm' and yos < 10 and qual == 1 )):
    sal = 10000 #salary
elif ( g == 'm' and yos < 10 and qual == 0 ):
    sal = 7000  #salary
elif ( g == 'f' and yos >= 10 and qual == 1 ):
    sal = 12000 #salary
elif ( g == 'f' and yos >= 10 and qual == 0 ):
    sal = 9000  #salary
elif ( g == 'f' and yos < 10 and qual == 1 ):
    sal = 10000 #salary
elif ( g == 'f' and yos < 10 and qual == 0 ):
    sal = 6000  #salary

#Result
print  "Salary of Employee = ", sal 
Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):
f 8 1
Salary of Employee =  10000
In [ ]: