# Chapter 8 - While and Do While Loops¶

## example 8.1, page no. 161¶

In :
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 :
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 :
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 :
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 :
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 :
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 :
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 : 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 :
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 :
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 [ ]: