Python Programming, 1/e. 1. Python Programming: An Introduction. To Computer
Science. Chapter 8. Loop Structures and Booleans. Python Programming, 1/e.
!"# $ % % %
& for
while
' $ while
!"# $
'
!"# $ ) % )%
*
% '
" " " ,
" *
% & $$
'
" " "
$$
' (
.
/
01 $ *
+
.
/
for
* 2 %$ ' for in : , $ " var 0 $$ 2 " % , % $
* 4
' -
01 $ *
* * * $ % % " ' 0 & % " ' ’ 0 0% * 0 * * " $"
" * 0 " '
3
.
/ 4 ’$ "% 6
01 $ *
.
4
"
'
01 $ *
Input the count of the numbers, n Initialize sum to 0 Loop n times
% % " % ' % ,
/
" " ' *
Input a number, x Add x to sum
'
Output average as sum/n 5
.
/
01 $ *
.
# average1.py # A program to average a set of numbers # Illustrates counted loop with accumulator
/
01 $ *
How many numbers do you have? 5 Enter a number >> 32 Enter a number >> 45 Enter a number >> 34 Enter a number >> 76 Enter a number >> 45
def main(): n = input("How many numbers do you have? ") sum = 0.0 for i in range(n): x = input("Enter a number >> ") sum = sum + x print "\nThe average of the numbers is", sum / n 8 & 9'9 sum/n % 6
The average of the numbers is 46.4
7
9
% #" 0 * % * " ’ " * ' 4 * * % 0 % * ' for % " % '
% 4 ’ 0 * '4
"
% " % ’ 0 * * *
$"
"
* % "
'
4
6
%
*
'
0
%
%
while : condition , if ' " ' " % '4
# 2
0 %
, % ' % "
)
0 *
%
%
'
,
' *
'
(
% ’
:
+
% % while 9 9
, %
while 2 $ " i" "% " % " ' for '
i = 0 while i > ") while x >= 0: sum = sum + x count = count + 1 x = input("Enter a number (negative to quit) >> ") print "\nThe average of the numbers is", sum / count
'
5
Enter Enter Enter Enter Enter Enter
a a a a a a
number number number number number number
(negative (negative (negative (negative (negative (negative
to to to to to to
quit) quit) quit) quit) quit) quit)
>> >> >> >> >> >>
32 45 34 76 45 -1
$
$ $ ‘ ’
% ’
* $ %* "
The average of the numbers is 46.4
7
%
%
* ' ’
– $
$
% " '
" ' (9
-
4
initialize sum to 0.0 initialize count to 0 input data item as a string, xStr while xStr is not empty convert xStr to a number, x add x to sum add 1 to count input next data item as a string, xStr Output sum / count
% '
D %
* " '=
$ )"
' 4
>“”A6
(
# average4.py # A program to average a set of numbers # Illustrates sentinel loop using empty string as sentinel
(
Enter Enter Enter Enter Enter Enter Enter
def main(): sum = 0.0 count = 0 xStr = raw_input("Enter a number ( to quit) >> ") while xStr != "": x = eval(xStr) sum = sum + x count = count + 1 xStr = raw_input("Enter a number ( to quit) >> ") print "\nThe average of the numbers is", sum / count
a a a a a a a
number number number number number number number
( ( ( ( ( ( (
to to to to to to to
quit) quit) quit) quit) quit) quit) quit)
>> >> >> >> >> >> >>
34 23 0 -25 -34.4 22.7
The average of the numbers is 3.38333333333
((
.
(+
. "
$
# average5.py # Computes the average of numbers listed in a file.
%
def main(): fileName = raw_input("What file are the numbers in? ") infile = open(fileName,'r') sum = 0.0 count = 0 for line in infile.readlines(): sum = sum + eval(line) count = count + 1 print "\nThe average of the numbers is", sum / count
$' 4 " +( "
% 0 %-9< % % %' (-
(3
3
.
. ’
E % 0 4
line = infile.readline() while line != "" #process line line = infile.readline()
$ % 6
'1
F
readline , % %' % % readline BC
* %< H ' *
G
" 0 * readline ' BI C 6J BC
* (5
.
(
8
# average6.py # Computes the average of numbers listed in a file.
*
* ** '4
if
def main(): fileName = raw_input("What file are the numbers in? ") infile = open(fileName,'r') sum = 0.0 count = 0 line = infile.readline() while line != "": sum = sum + eval(line) count = count + 1 line = infile.readline() print "\nThe average of the numbers is", sum / count
' * *
% "
% " " '
% >
A
(7
8
+9
8 $ * *
%)
sum
, $ count
* %
'
" %
"
sum = 0.0 count = 0 line = infile.readline() while line != "": #update sum and count for values in line line = infile.readline() print "\nThe average of the numbers is", sum/count
% "
"
' * %*
" ' * $ 4 +
" " count'
sum'
+
5
8
8 # average7.py # Computes the average of numbers listed in a file. # Works with multiple numbers on a line.
for xStr in string.split(line, ","): sum = sum + eval(xStr) count = count + 1
import string
for
8 line * $ " %
def main(): fileName = raw_input("What file are the numbers in? ") infile = open(fileName,'r') sum = 0.0 count = 0 line = infile.readline() while line != "": for xStr in string.split(line, ","): sum = sum + eval(xStr) count = count + 1 line = infile.readline() print "\nThe average of the numbers is", sum / count
'
+(
++
8
8 " %
F " F
' %
while %'
. for
–
F
% %
*
*
* * '
%
$ "
'
4
' %
,
%
% '
" +-
+3
* if ,
while " ' , False'
! , ,
'
True
$
*
% * G$ * $ >while x >= 0A
$
,
* –
,
2 2 '
' '
+5
+
!
!
if p1.getX() == p2.getX(): if p1.getY() == p2.getY(): # points are the same else: # points are different else: # points are different
and "
or
*
,
G
*0* ,
$ G 0 and or not'
' and or
* 6
+7
-9
! * 4
K,
and %* " %
,
, ,
,
"' /
. .
, $
/
.
% %$ ' $
"
. . .
.
/
" '
' * "
"
%P
$
and Q' -
-
K, or %* ,
K, ,
* '
,
/
/
,
.
or % % ' or ' B C
* * G '
" " **
. .
.
. -(
-+
7
!
!
not
% ,
4
'
0
not ,
"
,
'
, ' %
, %
'
. . --
-3
!
!
a or not b and c " $ < % % not and or' 2$ (a or ((not b) and c)) G &
% *
:*
$
%
*
)
%* and'
if p1.getX() == p2.getX() and p2.getY() == p1.getY(): # points are the same else: # points are different
* " *
"
%
'
' -5
-
! ’
*
2 "
'
$ '
:* ,
!
$' H %
0
* 0< bool True
False
$ '
A' *
*
$
$
%
0 == bool'
while response[0] == "y" or response[0] == "Y":
(
+
+
K,
K,
F
F
:*$ " ) " >
*
>>> bool(0) False >>> bool(1) True >>> bool(32) True >>> bool("Hello") True >>> bool("") False >>> bool([1,2,3]) True >>> bool([]) False
$ '. A &
% False True'
-
3
K,
K,
F
F 2
False * 0
) True'
!
!
, and
%, % ! *
,'
, or
%, ! *
,'
not ,
%, % ! *
True' False'
2
%
$ %
0
% %
'
' '
5
K,
K,
F
F "
, and ' ,
% " ' %
% % * % %
* 0 * % $ %, %
, *
"
* *
"
%
'
G ,
%, ,
' % '
'
% %
%
,' 7
79
-
K,
K,
F
F %
response[0] == "y" or "Y“
’
*
"
)
and * % , $
*
'
% 0 *' % , or * % * , '
:
G
2$
,
(response[0] == "y") or ("Y")
, ? 9@ 2 B C "
%or True % BHC " %* '
7
7
K,
K,
F
F * * % % $ "
%
" "
% %
" ans %
ans = raw_input("What flavor fo you want [vanilla]: ") if ans: flavor = ans else: flavor = "vanilla“
% "
" "
ans * *
# %
% '
'
7(
7+
K,
K,
F
F
4
$
6
ans = raw_input("What flavor fo you want [vanilla]: ") flavor = ans or "vanilla“
1
" ,
) True' ""
% % % %
*
& 'N 0
0 ’ *
6
*
6 flavor = raw_input("What flavor do you want [vanilla]:” ) or "vanilla"
7-
73
3