# Chapter 3 : Selections and Loops¶

## Example 3.1, Page No 48¶

In [1]:
dLimit = 500
iNo = int(raw_input("Specify Quantity : "))
dUnitPrice = float(raw_input("Specify Unit Price : "))
dGross = iNo * dUnitPrice
if(dGross > dLimit):
dDisc = 10
else:
dDisc = 0;
dNet = (100 - dDisc) * dGross / 100
print "Total Price : %d" % dNet

Specify Quantity : 100
Specify Unit Price : 10
Total Price : 900


## Example 3.2, Page No 54¶

In [6]:
dNo1 = int(raw_input("Enter Number 1 : "))
dNo2 = int(raw_input("Enter Number 2 : "))

print "1. Greatest"
print "2. Least"
print "3. Average"

iOpt = int(raw_input("Select : "))

if iOpt == 1:
if dNo1 > dNo2:
print dNo1
else:
print dNo2
print " is the greatest"
elif iOpt == 2:
if dNo1 < dNo2:
print dNo1
else:
print dNo2
print " is the least"
elif iOpt == 3:
print "The average is : " , (dNo1 + dNo2)/2
else:
print "wrong Choice"

Enter Number 1 : 10
Enter Number 2 : 15
1. Greatest
2. Least
3. Average
Select : 1
15
is the greatest


## Example 3.3, Page No 58¶

In [ ]:
iLimit = int(raw_input("Enter limit : "))
iSum = 0
for i in range(1,iLimit+1):
iSum = iSum + i

print "The Sum Is : " , iSum


# Example 3.4, Page No 61¶

In [1]:
print "Calculation Of Product"

for i in range(1,37):
for j in range(1,37):
if (i * j) == 36:
print i , " and " , j

Calculation Of Product
1  and  36
2  and  18
3  and  12
4  and  9
6  and  6
9  and  4
12  and  3
18  and  2
36  and  1


## Example 3.5, Page No 62¶

In [3]:
import random

iRoll = 0
iNoOfRolls = 0

while iRoll != 6:
iRoll =  random.randint(1,200) % 6 + 1
iNoOfRolls = iNoOfRolls + 1

print iNoOfRolls

8


## Example 3.6, Page No 63¶

In [4]:
# Do while concept is not in python thats way the program is implemented using WHILE
import random

iRoll = 0
iNoOfRolls = 0

while iRoll != 6:
iRoll =  random.randint(1,200) % 6 +1
iNoOfRolls = iNoOfRolls + 1

print iNoOfRolls

1


## Example 3.7, Page No 64¶

In [9]:
import random

iCounter = 0
iRoll1 = random.randint(1,200) % 6 + 1
iRoll2 = random.randint(1,200) % 6 + 1

while iRoll1 != iRoll2:
iRoll1 = random.randint(1,200) % 6 + 1
iRoll2 = random.randint(1,200) % 6 + 1
iCounter = iCounter + 1

print "The rolls were = " , iRoll1
print "Number of attempts = " , iCounter

The rolls were =  4
Number of attempts =  2


## Example 3.8, Page No 64¶

In [1]:
iSum = 0
i = 0
iNo = raw_input("Enter a number : ")

while iNo.isdigit():
iSum = iSum + int(iNo)
i = i + 1
iNo = raw_input("Enter one more number : ")

print "Average = " , (float)((iSum)/i)

Enter a number : 4
Enter one more number : 6
Enter one more number : 5
Enter one more number : c
Average =  5.0


## Example 3.9, Page No 66¶

In [2]:
import random
iNO = 0
for i in range(1,14):
iNo = random.randint(1,200) % 3
if iNo == 0:
print "1"
elif iNo == 1:
print "X"
elif iNO == 2:
print "2"

X
X
X
X
1
1
X
1


## Example 3.10, Page No 68¶

In [3]:
iNo = 0
iRoot = 1
while iNo < 2 and iRoot <=100:
LP = iRoot * (iRoot - 6) * (iRoot + 8)
if LP == 0:
iNo = iNo + 1
print iRoot
iRoot = iRoot + 1

6


## Example 3.11, Page No 69¶

In [4]:
import math

while 1 == 1:
dNo = int(raw_input("Enter a number : "))
if dNo <= 0:
break
print "The square root of the number is ",math.sqrt(dNo)

Enter a number : 100
The square root of the number is  10.0
Enter a number : 0

In [ ]: