3
Control Statements and
Program Development
Objectives
In this chapter, you’ll:
the built-in range function to
repeat actions for a sequence
of values.
Perform sentinel-controlled
iteration with while.
Create compound conditions
with the Boolean operators
and, or and not.
Stop looping with break.
Force the next iteration of a
PyCDS_03_ControlStatements.fm Page 1 Tuesday, May 21, 2019 11:19 AM
Note: Throughout the Instructor Solutions Manual, solutions are not provided for project,
research and challenge exercises—many of which are substantial and appropriate for term
projects, directed-study projects, capstone-course projects and thesis topics. Before assign-
ing a particular exercise for homework, instructors should check the IRC to be sure the
Exercises
Unless specified otherwise, use IPython sessions for each exercise.
3.2 (What’s Wrong with This Code?) What is wrong with the following code?
a = b = 7
print(‘a =’, a, ‘\nb =’, b)
First, answer the question, then check your work in an IPython session.
3.3 (What Does This Code Do?) What does the following program print?
for row in range(10):
for column in range(10):
print(‘< if row % 2 == 1 else ‘>’, end=)
print()
Answer: It displays the following output:
>>>>>>>>>>
<<<<<<<<<<
3.4 (Fill in the Missing Code) In the code below
for ***:
for ***:
print(‘@) # ERROR: This should be print(‘@’, end=)
print()
replace the *** so that when you execute the code, it displays two rows, each containing
seven @ symbols, as in:
@@@@@@@
@@@@@@@
Exercises 3
Answer:
for row in range(2):
3.6 (Turing Test) The great British mathematician Alan Turing proposed a simple test
to determine whether machines could exhibit intelligent behavior. A user sits at a comput-
er and does the same text chat with a human sitting at a computer and a computer oper-
ating by itself. The user doesn’t know if the responses are coming back from the human
or the independent computer. If the user can’t distinguish which responses are coming
from the human and which are coming from the computer, then it’s reasonable to say that
the computer is exhibiting intelligence.
Create a script that plays the part of the independent computer, giving its user a sim-
ple medical diagnosis. The script should prompt the user with ‘What is your problem?’
When the user answers and presses Enter, the script should simply ignore the user’s input,
then prompt the user again with ‘Have you had this problem before (yes or no)?’ If
the user enters ‘yes’, print ‘Well, you have it again. If the user answers ‘no’, print
‘Well, you have it now.’
Would this conversation convince the user that the entity at the other end exhibited
intelligent behavior? Why or why not?
3.28 (Intro to Data Science: Mean, Median and Mode) Calculate the mean, median and
mode of the values 9, 11, 22, 34, 17, 22, 34, 22 and 40. Suppose the values included an-
other 34. What problem might occur?
3.29 (Intro to Data Science: Problem with the Median) For an odd number of values,
to get the median you simply arrange them in order and take the middle value. For an even
number, you average the two middle values. What problem occurs if those two values are
different?
3.30 (Intro to Data Science: Outliers) In statistics, outliers are values out of the ordinary
and possibly way out of the ordinary. Sometimes, outliers are simply bad data. In the data
science case studies, we’ll see that outliers can distort results. Which of the three measures
of central tendency we discussed—mean, median and mode—is most affected by outliers?
Why? Which of these measures are not affected or least affected? Why?
Answer: Outliers would always change the mean, but they may have little or no
3.31 (Intro to Data Science: Categorical Data) Mean, median and mode work well with
numerical values. You can use them in calculations and arrange them in meaningful order.
Categorical values are descriptive names like Boxer, Poodle, Collie, Beagle, Bulldog and
4Control Statements and Program Development
Chihuahua. Normally, you don’t use these in calculations nor associate an order with
them. Which if any of the descriptive statistics are appropriate for categorical data?
PyCDS_03_ControlStatements.fm Page 4 Tuesday, May 21, 2019 11:19 AM