Chapter 3: Selection

Example 3.1, page no. 36

In [1]:
print "Enter two positive integers: ";
n = int(raw_input())
d = int(raw_input())
if (n%d):
    print "%d is not divisible by %d" %(n,d)
Enter two positive integers: 
66
7
66 is not divisible by 7

Example 3.2, page no. 37

In [2]:
print "Enter two positive integers: ";
n = int(raw_input())
d = int(raw_input())
if (n%d):
    print "%d is not divisible by %d" %(n,d)
else:
    print "%d is divisible by %d" %(n,d)
Enter two positive integers: 
56
7
56 is divisible by 7

Example 3.3, page no. 38

In [3]:
print "Enter two integers: "
m = int(raw_input())
n = int(raw_input())

if (m < n):
    print "%d is the minimum." %m
else:
    print "%d is the minimum." %n
Enter two integers: 
77
55
55 is the minimum.

Example 3.4, page no. 38

In [1]:
print "Enter an integer: "
n = int(raw_input())
if (n == 22):
    print "%d = 22" %n
else: 
    print "%d != 22" %n
Enter an integer: 
22
22 = 22

Example 3.5, page no. 39

In [5]:
print "Enter three integers: "
n1 = int(raw_input())
n2 = int(raw_input())
n3 = int(raw_input())

m=n1
# now min <= n1
if (n2 < m):
    m = n2 # now min <= n1 and min <= n2
if (n3 < m):
    m = n3  # now min <= n1, min <= n2, and min <= n3
print "Their minimum is %d" % m
Enter three integers: 
77
33
55
Their minimum is 33

Example 3.6, page no. 39

In [6]:
print "Enter two integers: "
x = int(raw_input())
y = int(raw_input())

if (x > y):
    temp=x
    x = y
    y = temp
    

print "%d <= %d" %(x,y)
Enter two integers: 
66
44
44 <= 66

Example 3.7, page no. 40

In [7]:
n=44
print "n = %d" % n 
if True:
    # scope extends over 4 lines
    print "Enter an integer: "
    n = int(raw_input())
    print  "n = %d" % n 

if True:
    print  "n = %d" % n 
    # the n that was declared first
if True:
    print  "n = %d" % n 

print  "n = %d" % n 
n = 44
Enter an integer: 
77
n = 77
n = 77
n = 77
n = 77

Example 3.8, page no. 41

In [8]:
print "Enter three integers: "
n1  = int(raw_input())
n2  = int(raw_input())
n3  = int(raw_input())
if (n1 <= n2 and n1 <= n3):
    print "Their minimum is %d" % n1
    
if (n2 <= n1 and n2 <= n3):
    print  "Their minimum is %d " % n2 
if (n3 <= n1 and n3 <= n2):
    print "Their minimum is  %d" % n3 
Enter three integers: 
77
33
55
Their minimum is 33 

Example 3.9, page no. 41

In [9]:
print "Are you enrolled (y/n): "
ans = raw_input()
if (ans == 'Y' or ans == 'y'):
    print "You are enrolled.\n"
else: 
    print "You are not enrolled.\n"
Are you enrolled (y/n): 
y
You are enrolled.

Example 3.10, page no. 42

In [10]:
print "Enter two positive integers: ";
n = int(raw_input())
d = int(raw_input())

if (d != 0 and n%d == 0): 
    print "%d divides %d" %(d,n)
else:
    print "%d does not divide %d"% (d,n)
Enter two positive integers: 
33
6
6 does not divide 33

Example 3.11, page no. 43

In [11]:
print  "Enter three integers: "
n1 = int(raw_input())
n2 = int(raw_input())
n3 = int(raw_input())

if (n1 >= n2 >= n3):
    print "max = x"
Enter three integers: 
0
0
1

Example 3.12, page no. 43

In [12]:
print  "Enter two positive integers: "
n = int(raw_input())
d = int(raw_input())

if (d != 0):
    if (n%d == 0):
        print d,
        print " divides %d" % n 
    else:
        print "%d does not divide %d" %(d,n)
else:
    print '%d does not divide %d '%(d,n)
Enter two positive integers: 
55
44
44 does not divide 55

Example 3.13, page no. 44

In [13]:
print "Enter three integers: "
n1 = int(raw_input())
n2 = int(raw_input())
n3 = int(raw_input())
if (n1 < n2):
    if (n1 < n3):
        print "Their minimum is : %d" % n1
    else:
        print "Their minimum is : %d" % n3
else: # n1 >= n2
    if (n2 < n3):
        print "Their minimum is : %d" % n2
    else:
        print "Their minimum is %d" % n3
Enter three integers: 
77
33
55
Their minimum is : 33

Example 3.14, page no. 44

In [14]:
print "Pick a number from 1 to 8." 
answer = int(raw_input())
print "Is it less than 5? (y|n): "
answer = raw_input()
if (answer == 'y'): # 1 <= n <= 4
    print "Is it less than 3? (y|n): "
    answer = raw_input()    
    if (answer == 'y'): # 1 <= n <= 2
        print "Is it less than 2? (y|n): "
        answer = raw_input()
        if (answer == 'y'):
            print "Your number is 1."
        else:
            print "Your number is 2."
    else: # 3 <= n <= 4
        print "Is it less than 4? (y|n): "
        answer = raw_input()
        if (answer == 'y'):
            print "Your number is 3."
        else:
                print "Your number is 4."
else: # 5 <= n <= 8
    print "Is it less than 7? (y|n): "
    answer = raw_input()
    if (answer == 'y'): # 5 <= n <= 6
        print "Is it less than 6? (y|n): "
        answer = raw_input()
        if (answer == 'y'):
            print "Your number is 5."
        else:
            print "Your number is 6." 
    else: # 7 <= n <= 8
        print "Is it less than 8? (y|n): "
        answer = raw_input()
        if (answer == 'y'):
            print "Your number is 7." 
        else:
            print "Your number is 8."
Pick a number from 1 to 8.
6
Is it less than 5? (y|n): 
n
Is it less than 7? (y|n): 
y
Is it less than 6? (y|n): 
n
Your number is 6.

Example 3.15, page no. 46

In [15]:
language = raw_input("Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): ")

if (language == 'e'): 
    print "Welcome to ProjectEuclid."
elif (language == 'f'):
    print "Bon jour, ProjectEuclid."
elif (language == 'g'):
    print "Guten tag, ProjectEuclid."
elif (language == 'i'):
    print "Bon giorno, ProjectEuclid."
elif (language == 'r'):
    print "Dobre utre, ProjectEuclid."
else:
    print "Sorry; we don't speak your language."
Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): i
Bon giorno, ProjectEuclid.

Example 3.16, page no. 47

In [16]:
score = int(raw_input("Enter your test score: "))
a = int(score/10)
if a == 10 or a == 9:
    print "Your grade is an A."
elif a == 8:
    print "Your grade is a B." 
elif a == 7:
    print "Your grade is a C." 
elif a == 6:
    print "Your grade is a D."
elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:
    print "Your grade is an F." 
else:
    print "Error: score is out of range.\n"
Enter your test score: 83
Your grade is a B.

Example 3.17, page no. 47

In [17]:
score = int(raw_input("Enter your test score: "))
a = int(score/10)
if a == 10 or a == 9:
    print "Your grade is an A."
elif a == 8:
    print "Your grade is a B." 
elif a == 7:
    print "Your grade is a C." 
elif a == 6:
    print "Your grade is a D."
elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:
    print "Your grade is an F." 
else:
    print "Error: score is out of range.\n"

print "Goodbye."
Enter your test score: 83
Your grade is a B.
Goodbye.

Example 3.18, page no. 48

In [18]:
score = int(raw_input("Enter your test score: "))
a = int(score/10)
if a == 10 or a == 9:
    print "Your grade is an A."
elif a == 8:
    print "Your grade is a B." 
elif a == 7:
    print "Your grade is a C." 
elif a == 6:
    print "Your grade is a D."
elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:
    print "Your grade is an F." 
else:
    print "Error: score is out of range.\n"

print "Goodbye."
Enter your test score: 83
Your grade is a B.
Goodbye.

Example 3.19, page no. 49

In [19]:
print "Enter two integers: "
m = int(raw_input())
n =  int(raw_input())
print min(m,n),
print 'is the minimum'
Enter two integers: 
33
55
33 is the minimum