Chapter 8 - While and Do While Loops

example 8.1, page no. 161

In [1]:
for num in range(1, 11):
    print num,
1 2 3 4 5 6 7 8 9 10

example 8.2, page no. 161

In [2]:
num = 1
while num <=10:
    print num,
    num += 1
1 2 3 4 5 6 7 8 9 10

example 8.3, page no. 162

In [1]:
num = 0
while (num <= 10):
    print num,
    num += 1
0 1 2 3 4 5 6 7 8 9 10

example 8.4, page no. 163

In [4]:
print "Enter a positive number: ",
num = int(raw_input())
while (num <= 0):
    print "Number must be positive; please retry: ",
    num = int(raw_input())
print "The number you entered is ", num, " "
Enter a positive number: 4
 The number you entered is  4  

example 8.5, page no. 164

In [5]:
print "Enter a positive number: ",
num = int(raw_input())
while (num <= 0):
    print "Number must be positive; try again (Y/N): ",
    choice = raw_input()
    if (choice == 'Y'):
        print "Enter number: ",
        num = int(raw_input())
    else:
        break
print "The number you entered is ", num, " "
Enter a positive number: 0
 Number must be positive; try again (Y/N): Y
 Enter number: 7
 The number you entered is  7  

example 8.6, page no. 165

In [6]:
quit = False
print "Enter a positive number: ",
num = int(raw_input())
while (num <= 0 and quit == False):
    print "Number must be positive; try again (Y/N): ",
    choice = raw_input()
    if (choice is 'Y'):
        print "Enter number: ",
        num = int(raw_input())
    else:
        quit = True
if (quit == False):
    print "The number you entered is ", num, " "
else:
    print "You did not enter a positive number"
Enter a positive number: 3
 The number you entered is  3  

example 8.7, page no. 168

In [7]:
quit = False
while(True):
    print "Enter a positive number: ",
    num = int(raw_input())
    if (num > 0):
        break
    else:
        print "Number must be positive; try again (Y/N): ",
        choice = raw_input()
        if (choice != 'Y'):
            quit = True
            break
if (quit == False):
    print "The number you entered is ", num, " "
else:
    print "You did not enter a positive number"
Enter a positive number: -1
 Number must be positive; try again (Y/N): N
 You did not enter a positive number

example 8.8, page no. 169

In [ ]:
counter = 0
total = 0
print "How many items do you want to buy: ",
num = int(raw_input())
while(counter < num):
    counter += 1
    if (counter % 13 == 0):
        continue
    total += 3
print "Total for ", num, " items is $", total

example 8.9, page no. 169

In [10]:
counter = 0
total = 0
print "How many items do you want to buy: ",
num = int(raw_input())
keepgoing = True
while(counter < num):
    counter += 1
    if (not(counter % 13 == 0)):
        total += 3;
print "Total for ", num, " items is $", total
How many items do you want to buy: 4
 Total for  4  items is $ 12

example 8.10, page no. 170

In [11]:
x = 0
while(x < 5):
    x += 1
    y = 0
    while(y < 5):
        y += 1
        print "X",
    print '\n'
X X X X X 

X X X X X 

X X X X X 

X X X X X 

X X X X X 

example 8.11, page no. 171

In [12]:
quit = False
print "Enter a positive number: ",
num = int(raw_input())
while (num <= 0 and quit == False):
    print "Number must be positive; try again (Y/N): ",
    choice = raw_input()
    if (choice is 'Y'):
        print "Enter number: ",
        num = int(raw_input())
    else:
        quit = True
if (quit == False):
    print "The number you entered is ", num, " "
else:
    print "You did not enter a positive number"
Enter a positive number: 3
 The number you entered is  3  
In [ ]: