Chapter 4 Looping Structures

Example 4.1 , Page No.88

In [1]:
x=0
print "The value of x is ",x
x+=1
print "The value of x is ",x
The value of x is  0
The value of x is  1

Example 4.2, Page No.89

In [3]:
x=0
y=0
print "The value of y is ", y+1
print "The value of x is ", x+1
The value of y is  1
The value of x is  1

Example 4.3, Page No.89

In [5]:
x=0
y=1
x=(y)*2
print "The Value of x ",x
x=0
y=1
x=(y+1)*2
print "The Value of x ",x
The Value of x  2
The Value of x  4

Example 4.4, Page No.90

In [6]:
x=0
y=0
x=(y)*4
print "The Value of x", x
y=0
x=(y+1)*4
print "The Value of x", x
The Value of x 0
The Value of x 4

Example 4.5, Page No.92

In [7]:
x=1
y=1
x=(y)*4
print "The value of x is ",x
y=1
x=(y-1)*4
print "The value of x is ",x
The value of x is  4
The value of x is  0

Example 4.6, Page No. 93

In [8]:
x=1
y=2
x=y*x+1
print "The value of x is: ",x
x=1
y=2
x+=y*x+1
print "The value of x is: ",x
The value of x is:  3
The value of x is:  4

Example 4.7, Page No.94

In [9]:
x=1
y=2
x=y*x+1
print "The value of x is: ",x
x=1
y=2
x-=y*x+1
print "The value of x is: ",x
The value of x is:  3
The value of x is:  -2

Example 4.8, Page No.95

In [11]:
x=0
while (x<10):
    print "The value of x is ",x
    x=x+1
The value of x is  0
The value of x is  1
The value of x is  2
The value of x is  3
The value of x is  4
The value of x is  5
The value of x is  6
The value of x is  7
The value of x is  8
The value of x is  9

Example 4.9, Page No.97

In [13]:
iSelection=0
while (iSelection!=4):
    print "1\tDeposit Funds"
    print "2\tWithdraw Funds"
    print "3\tPrint Balance"
    print "4\tQuit"
    iSelection=int(raw_input("Enter Your Selection(1-4): "))
print "Thank you"
1	Deposit Funds
2	Withdraw Funds
3	Print Balance
4	Quit
Enter Your Selection(1-4): 1
1	Deposit Funds
2	Withdraw Funds
3	Print Balance
4	Quit
Enter Your Selection(1-4): 4
Thank you

Example 4.10, Page No.99

In [1]:
x=9
while (x<10):
    print "This printf statement is executed at least once"
    x=x+1
while (x<10):
    print "This printf statement is never executed "
    x=x+1
This printf statement is executed at least once

Example 4.11, Page No.100

In [2]:
for x in range(10,5,-1):
    print "The value of x is ",x
The value of x is  10
The value of x is  9
The value of x is  8
The value of x is  7
The value of x is  6

Example 4.12, Page No.101

In [2]:
import random
iNumQuestions=int(raw_input("Enter the number of questions to ask: "))
for x in range(iNumQuestions):
    iRndNum1=(random.randint(0,100)%10)+1
    iRndNum2=(random.randint(0,100)%10)+1
    iResponse=int(raw_input("what is "+ str(iRndNum1)+" * "+str(iRndNum2)+" : "))
    if(iResponse==(iRndNum1*iRndNum2)):
        print "Correct"
    else:
        print "The Corrent answer was ", iRndNum1*iRndNum2
Enter the number of questions to ask: 2
what is 6 * 9 : 54
Correct
what is 8 * 5 : 32
The Corrent answer was  40

Example 4.13, Page No.102

In [3]:
for x in range(10,5,-1):
    if x==7:
        break
print x
7

Example 4.14, Page No.103

In [4]:
for x in range(10,5,-1):
    if x==7:
        continue
    print x
10
9
8
6

Example 4.15, Page No.104

In [6]:
for x in range(25):
    print "\n"

















































Example 4.16, Page No. 106

In [1]:
import time
import random
import os
cYesNo=raw_input("Play a game of Concentration?(y or n)")
if(cYesNo=='y' or cYesNo=='Y'):
    i1=random.randint(0,100) % 100
    i2=random.randint(0,100) % 100
    i3=random.randint(0,100) % 100
    print "Concentrate on the next three numbers\n",i1,i2,i3
    iCurrentTime=int(round(time.time()*1000))
    iElapsedTime=int(round(time.time()*1000))
    while((iCurrentTime-iElapsedTime)<3):
        iElapsedTime=int(round(time.time()*1000))
    os.system('cls')
    iResp1=int(raw_input("Enter a number:"))
    iResp2=int(raw_input("Enter a number:"))
    iResp3=int(raw_input("Enter a number:"))
    
    if(i1==iResp1 and i2==iResp2 and i3==iResp3):
        print "Congratulations!"
    else:
        print "Sorry, Correct numbers were ",i1,i2,i3
Play a game of Concentration?(y or n)n