Chapter 4: Program Control Statements

Example 4.1, Page Number: 58

In [10]:
import random

#Variable declaration and initialization
magic = random.randint(0,100)           #Number which the user has to guess
guess = 10                              #Number which the user guesses

if guess == magic:
    print "***Right***"                 #Result


                  

Example 4.2, Page Number: 59

In [8]:
import random

#Variable decleration and initialization
magic=random.randint(0,100)             #Number to be guessed
guess = 10                              #Number the user guesses

#Result
if guess == magic:
    print "***Right***"
else:
    print "... Sorry, you're wrong."



    
... Sorry, you're wrong.

Example 4.3, Page Number: 60

In [11]:
#Variable decleration and initialization
a = 30                                   #User input for the two nos.
b = 10

#Calculation and Result
if b:
    print a/b
else:
    print "Cannot divide by zero."
3

Example 4.4, Page Number: 61

In [ ]:
import random

#Variable decleration 
magic = random.randint(0,100)               #Numbr to be guessed
guess = 10                                  #Number the user guesses

#Result
if guess == magic:
    print "***Right***"
    print magic," is the magic number."
else:
    print "... Sorry, you're wrong"     
    if(guess>magic):                        #use a nested if statement
        print "Your guess is too high"
    else:
        print "Your guess is too low"

        
    

    
                

Example 4.5, Page Number: 62

In [13]:
for x in range(6):
    if x==1:                        #using if-else ladder in a for loop
        print "x is one"
    elif x==2:
        print "x is two"
    elif x==3:
        print "x is three"
    elif x==4:
        print "x is four"
    else:
        print "x is not between 1 nd 4"
        
    
        
x is not between 1 nd 4
x is one
x is two
x is three
x is four
x is not between 1 nd 4

Example 4.6, Page Number: 63

In [14]:
import math

for num in range(100):
    sq_root = math.sqrt(float(num))         #Calculation
    print num," ",sq_root                   #Result
    
0   0.0
1   1.0
2   1.41421356237
3   1.73205080757
4   2.0
5   2.2360679775
6   2.44948974278
7   2.64575131106
8   2.82842712475
9   3.0
10   3.16227766017
11   3.31662479036
12   3.46410161514
13   3.60555127546
14   3.74165738677
15   3.87298334621
16   4.0
17   4.12310562562
18   4.24264068712
19   4.35889894354
20   4.472135955
21   4.58257569496
22   4.69041575982
23   4.79583152331
24   4.89897948557
25   5.0
26   5.09901951359
27   5.19615242271
28   5.29150262213
29   5.38516480713
30   5.47722557505
31   5.56776436283
32   5.65685424949
33   5.74456264654
34   5.83095189485
35   5.9160797831
36   6.0
37   6.0827625303
38   6.16441400297
39   6.2449979984
40   6.32455532034
41   6.40312423743
42   6.48074069841
43   6.5574385243
44   6.63324958071
45   6.7082039325
46   6.78232998313
47   6.8556546004
48   6.92820323028
49   7.0
50   7.07106781187
51   7.14142842854
52   7.21110255093
53   7.28010988928
54   7.34846922835
55   7.4161984871
56   7.48331477355
57   7.54983443527
58   7.61577310586
59   7.68114574787
60   7.74596669241
61   7.81024967591
62   7.87400787401
63   7.93725393319
64   8.0
65   8.0622577483
66   8.12403840464
67   8.18535277187
68   8.24621125124
69   8.30662386292
70   8.36660026534
71   8.42614977318
72   8.48528137424
73   8.54400374532
74   8.60232526704
75   8.66025403784
76   8.71779788708
77   8.77496438739
78   8.83176086633
79   8.88819441732
80   8.94427191
81   9.0
82   9.05538513814
83   9.11043357914
84   9.16515138991
85   9.21954445729
86   9.2736184955
87   9.32737905309
88   9.38083151965
89   9.43398113206
90   9.48683298051
91   9.53939201417
92   9.59166304663
93   9.64365076099
94   9.69535971483
95   9.74679434481
96   9.79795897113
97   9.8488578018
98   9.89949493661
99   9.94987437107

Example 4.7, Page Number: 64

In [1]:
for i in xrange(100,-100,-5):       
    print i,
    
100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0 -5 -10 -15 -20 -25 -30 -35 -40 -45 -50 -55 -60 -65 -70 -75 -80 -85 -90 -95

Example 4.8, Page Number: 66

In [11]:
#Variable decleration
x=0

#Loop till 123 is entered
while x!=123:
    print "Enter a number: "
    x = 123
    
Enter a number: 

Example 4.9, Page Number: 68

In [19]:
 
print "Help on:"
print "1. for"
print "2. if"
print "3. while"


choice = 2                     #Choice of user

if choice==1:                  #Executing users choice with if-else
    print "for is c++'s most versatile loop."
elif choice==2:
    print "if is c++'s conditional branch statement."
elif choice==3:
    print "switch is C++'s multiway branch statement. "
else:
    print "You must enter a number between 1 and 3."
    
Help on:
1. for
2. if
3. while
if is c++'s conditional branch statement.

Example 4.10, Page Number: 69

In [20]:
for i in range(5):
    if i==0:
        print "less than 1"
    elif i==1:
        print "less than 2"
    elif i==2:
        print "less than 3"
    elif i==3:
        print "less than 4"
    elif i==4:
        print "less than 5"
less than 1
less than 2
less than 3
less than 4
less than 5

Example 4.11, Page Number: 71

In [21]:
 
#Variable decleration
ch = 32

#Loop to print the characters
for ch in range(128):
    print chr(ch)
    









	























 
!
"
#
$
%
&
'
(
)
*
+
,
-
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
_
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~


Example 4.12, Page Number: 72

In [22]:
#Variable decleration
len = 5

while (len>0 & len<80):
    print "."
    len-=1
.
.
.
.
.

Example 4.13, Page Number: 73

In [9]:
#Variable Declaration
num=95

while True:
    print "Enter a number(100 to stop): "
    num+=1                               #User input, incrementing num till it reaches 100
    if(num==100):                        #Condition check to stop loop
        break
Enter a number(100 to stop): 
Enter a number(100 to stop): 
Enter a number(100 to stop): 
Enter a number(100 to stop): 
Enter a number(100 to stop): 

Example 4.14, Page Number: 73

In [26]:
import random

#Variable decleration
magic = random.randint(0,100)               #Number which the user has to guess
low=0
high=100

#Play thr magic number game
while True:
    guess = random.randint(low,high)         #Number which the user guesses
    if guess==magic:                             
        print "**Right**"
        print magic," is the magic number."
        break
    else:
        print "...Sorry, you're wrong."
        if(guess>magic):
            print "Your guess is too high."
            high=guess
        else:
            print "Your guess is too low."
            low=guess
    
    
...Sorry, you're wrong.
Your guess is too low.
...Sorry, you're wrong.
Your guess is too high.
...Sorry, you're wrong.
Your guess is too low.
...Sorry, you're wrong.
Your guess is too high.
...Sorry, you're wrong.
Your guess is too low.
...Sorry, you're wrong.
Your guess is too high.
...Sorry, you're wrong.
Your guess is too low.
...Sorry, you're wrong.
Your guess is too high.
...Sorry, you're wrong.
Your guess is too low.
...Sorry, you're wrong.
Your guess is too low.
...Sorry, you're wrong.
Your guess is too high.
...Sorry, you're wrong.
Your guess is too high.
...Sorry, you're wrong.
Your guess is too high.
...Sorry, you're wrong.
Your guess is too low.
**Right**
72  is the magic number.

Example 4.15, Page Number: 74

In [2]:
for x in range(100+1):
    if x%2:               #Condition check to continue the loop
        continue
    print x,
        
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

Example 4.16, Page Number: 75

In [3]:
for t in range(100):
    if t==10:               #Condition check to break out of the loop
        break
    print t,
        
0 1 2 3 4 5 6 7 8 9

Example 4.17, Page Number: 75

In [5]:
for t in range(100):
    count = 1                   
    while True:
        print count,
        count+=1
        if count==10: 
            break               #Breaks from the inner loop
    print
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 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4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

Example 4.18, Page Number: 76

In [30]:
for i in range(2,1000):
    j=2
    for j in range(2,i/j):          #Nested for loop
        if i%j == False:            #Check for prime no.
            break
    if j>(i/j):
            print i," is prime."    #Result
2  is prime.
3  is prime.
11  is prime.
13  is prime.
17  is prime.
19  is prime.
23  is prime.
29  is prime.
31  is prime.
37  is prime.
41  is prime.
43  is prime.
47  is prime.
53  is prime.
59  is prime.
61  is prime.
67  is prime.
71  is prime.
73  is prime.
79  is prime.
83  is prime.
89  is prime.
97  is prime.
101  is prime.
103  is prime.
107  is prime.
109  is prime.
113  is prime.
127  is prime.
131  is prime.
137  is prime.
139  is prime.
149  is prime.
151  is prime.
157  is prime.
163  is prime.
167  is prime.
173  is prime.
179  is prime.
181  is prime.
191  is prime.
193  is prime.
197  is prime.
199  is prime.
211  is prime.
223  is prime.
227  is prime.
229  is prime.
233  is prime.
239  is prime.
241  is prime.
251  is prime.
257  is prime.
263  is prime.
269  is prime.
271  is prime.
277  is prime.
281  is prime.
283  is prime.
293  is prime.
307  is prime.
311  is prime.
313  is prime.
317  is prime.
331  is prime.
337  is prime.
347  is prime.
349  is prime.
353  is prime.
359  is prime.
367  is prime.
373  is prime.
379  is prime.
383  is prime.
389  is prime.
397  is prime.
401  is prime.
409  is prime.
419  is prime.
421  is prime.
431  is prime.
433  is prime.
439  is prime.
443  is prime.
449  is prime.
457  is prime.
461  is prime.
463  is prime.
467  is prime.
479  is prime.
487  is prime.
491  is prime.
499  is prime.
503  is prime.
509  is prime.
521  is prime.
523  is prime.
541  is prime.
547  is prime.
557  is prime.
563  is prime.
569  is prime.
571  is prime.
577  is prime.
587  is prime.
593  is prime.
599  is prime.
601  is prime.
607  is prime.
613  is prime.
617  is prime.
619  is prime.
631  is prime.
641  is prime.
643  is prime.
647  is prime.
653  is prime.
659  is prime.
661  is prime.
673  is prime.
677  is prime.
683  is prime.
691  is prime.
701  is prime.
709  is prime.
719  is prime.
727  is prime.
733  is prime.
739  is prime.
743  is prime.
751  is prime.
757  is prime.
761  is prime.
769  is prime.
773  is prime.
787  is prime.
797  is prime.
809  is prime.
811  is prime.
821  is prime.
823  is prime.
827  is prime.
829  is prime.
839  is prime.
853  is prime.
857  is prime.
859  is prime.
863  is prime.
877  is prime.
881  is prime.
883  is prime.
887  is prime.
907  is prime.
911  is prime.
919  is prime.
929  is prime.
937  is prime.
941  is prime.
947  is prime.
953  is prime.
967  is prime.
971  is prime.
977  is prime.
983  is prime.
991  is prime.
997  is prime.

Example 4.19, Page Number: 78

In [6]:
import random

#Variable declaration
magic = random.randint(0,100)           #Number to be guessed
i=1
high=100
low=0

#Function to play the magic number game
def play(m):
    low=0
    high=100
    for t in range(100):
        x =  random.randint(low,high) #Number guessed by the user
        if x==m:
            print "***Right***"
            return
        else:
            if(x<m):
                print "Too low."
                low=x
            else:
                print "Too high."
                high=x
    print "You've used up all your guesses"          

#Menu
while True:
    print "1.Get a new magic number."                   
    print "2.Play"
    print "3.Quit"
    while True:
        option = i  
        if option>=1 and option<=3:
            break
    if option==1:
        magic=random.randint(0,100)     #Number to be guessed
    elif option==2:
        play(magic)                     #Calls the function play
    elif option==3:
        print "Goodbye"                 
        break
    i+=1                                #increments i such that the 3 options get selected sequentially
1.Get a new magic number.
2.Play
3.Quit
1.Get a new magic number.
2.Play
3.Quit
Too low.
Too low.
Too low.
Too low.
Too high.
Too low.
Too high.
Too low.
Too high.
Too high.
Too high.
Too high.
Too low.
***Right***
1.Get a new magic number.
2.Play
3.Quit
Goodbye
In [ ]: