• Home
  • Textbooks
  • The Practice of Computing Using Python
  • Control

The Practice of Computing Using Python

William F. Punch, Richard Enbody

Chapter 3

Control - all with Video Answers

Educators


Chapter Questions

04:05

Problem 1

How many three-digit numbers are divisible by 17 ? Write a program to print them.

Raushan Kumar
Raushan Kumar
Numerade Educator
04:35

Problem 2

In your own words, describe what the statements continue and break do in loops.

Willis James
Willis James
Numerade Educator

Problem 3

Explain the difference between "is" and " $==$ ". Give an example (different than that used in the chapter) that illustrates items that return Fal se for "is" and True for " $==$ ".

Check back soon!
02:35

Problem 4

In an earlier set of exercises, you were asked to calculate one's BMI. Augment that program by printing out where that BMI fits in the CDC standard weight status categories:
$$
\begin{array}{|c|c|}
\hline \text { BMI } & \text { Weight Status } \\
\hline \text { Below 18.5 } & \text { Underweight } \\
18.5-24.9 & \text { Normal } \\
25.0-29.9 & \text { Overweight } \\
30.0 \text { and above } & \text { Obese } \\
\hline
\end{array}
$$

Charles Carter
Charles Carter
Numerade Educator
01:13

Problem 5

Control:
if my var % 2:
if my var**3 != 27:
my var = my var + 4 # Assignment 1
else:
my var /= 1.5 # Assignment 2
else:
if my var <= 10:
my var *= 2 # Assignment 3
else:
my var -= 2 # Assignment 4
print(my var)
(a) Find four values of my_var so each of the four assignment statements will be executed: each value should cause one assignment statement to be executed.
(b) Find four ranges of my_var values that will cause each of the four assignment statements to be executed.

Mohamed Mohamed
Mohamed Mohamed
Numerade Educator

Problem 6

Write a for loop that will print "pbil" when "alphebetical" is the input.

Check back soon!
00:51

Problem 7

Consider the Python function range $(a, b)$. Label these statements as True or False.
$\bullet$ Value " $a$ " is included in the range.
$\bullet$ Value " $b$ " is included in the range.

Megan Mcfarland
Megan Mcfarland
Numerade Educator

Problem 8

What is an iterator? Give two examples of iterators.

Check back soon!
01:30

Problem 9

In this exercise you will convert knuts to sickles and galleons (the currency of the Harry Potter novels). Perform the calculation, and print only non-zero values. That is, if there are not enough knuts for there to be one sickle, then " 0 sickle" should not be printed. (There are 29 knuts in one sickle and 17 sickles in one galleon.)

Adam Conner
Adam Conner
Numerade Educator

Problem 10

Write a short program that will:
$\bullet$ prompt the user for a number
$\bullet$ print out whether the number is a perfect square
$\bullet$ prompt the user for another number if the input was not a perfect square

Check back soon!
03:20

Problem 11

Sum of consecutive integers
(a) Write a program that prompts for an integer-let's call it $\mathrm{X}$-and then finds the sum of $X$ consecutive integers starting at 1 . That is, if $X=5$, you will find the sum of $1+2+3+4+5=15$
(b) Modify your program by enclosing your loop in another loop so that you can find consecutive sums. For example, if 5 is entered, you will find five sums of consecutive numbers:
$$
\begin{array}{ll}
1 & =1 \\
1+2 & =3
\end{array}
$$
CONTROL
$$
\begin{array}{ll}
1+2+3 & =6 \\
1+2+3+4 & =10 \\
1+2+3+4+5 & =15
\end{array}
$$
Print only each sum, not the arithmetic expression.
(c) Modify your program again to only print sums if the sum is divisible by the number of operands. For example, with the sum $1+2+3+4+5=15$, there are five operands and the sum, 15, is divisble by 5 , so that sum will be printed. (Do you notice a pattern?)

Faizanullah Kazmi
Faizanullah Kazmi
Numerade Educator
07:24

Problem 12

In this chapter is a program that checks for perfect numbers. If you wanted to check many numbers (a large range of numbers) to see if any were perfect, then the program we wrote might not be the best. What could we do to improve it? For example, how would we output only perfect numbers but just keep count of the deficient and abundant numbers? Do we need to check every number from 2 to number-1?

Darren Wilson
Darren Wilson
Numerade Educator
01:27

Problem 13

Write a program that prompts for an integer and prints the integer, but if something other than an integer is input, the program keeps asking for an integer. Here is a sample session:
Input an integer: abc
Error: try again. Input an integer: $4 \mathrm{a}$
Error: try again. Input an integer: 2.5
Error: try again. Input an integer: 123
The integer is : 123

James Kiss
James Kiss
Numerade Educator
02:57

Problem 14

Write a program to generate the following arithmetic examples. Hints:
(1) Divide and conquer: what simpler problem do you need to solve?
(2) Consider using strings to build numbers and then convert.
(3) The range iterator may be helpful.
$1 * 8+1=9$
$12 * 8+2=98$
$123 * 8+3=987$
$1234 * 8+4=9876$
$12345 * 8+5=98765$
$123456 * 8+6=987654$
$1234567 * 8+7=9876543$
$12345678 * 8+8=98765432$
$123456789 * 8+9=987654321$
$1 * 9+2=11$
$12 * 9+3=111$
$123 * 9+4=1111$
$1234 * 9+5=11111$
$12345 * 9+6=111111$
$123456 * 9+7=1111111$
$1234567 * 9+8=11111111$
$12345678 * 9+9=111111111$
$123456789 * 9+10=1111111111$
$9 * 9+7=88$ $98 * 9+6=888$ $987 * 9+5=8888$ $9876 * 9+4=88888$ $98765 * 9+3=888888$ $987654 * 9+2=8888888$ $9876543 * 9+1=88888888$ $98765432 * 9+0=888888888$
$1 * 1=1$
$11 * 11=121$
$111 * 111=12321$
$1111 * 1111=1234321$
$11111 * 11111=123454321$
$111111 * 111111=12345654321$
$1111111 * 1111111=1234567654321$
$11111111 * 11111111=123456787654321$
$111111111 * 111111111=12345678987654321$

James Kiss
James Kiss
Numerade Educator
05:09

Problem 15

Write a program that checks to see if a number $N$ is prime. A simple approach checks all numbers from 2 up to $N$, but after some point numbers are checked that need not be checked. For example, numbers greater than $\sqrt{N}$ need not be checked. Write a program that checks for primality and avoids those unnecessary checks. Remember to import the math module.

Willis James
Willis James
Numerade Educator
02:57

Problem 16

The perfect-number example in this chapter uses two while loops. Rewrite the program replacing the two while loops with two for loops and range.

Mayank Tripathi
Mayank Tripathi
Numerade Educator

Problem 17

Here is an example of a while loop. There is an error; can you find the error in the while loop? Try to figure out what could be wrong by thinking through the while loop. Referring to the flow-chart figures in the chapter should help. Check to see if you are right by using Python!
sentence= "that car was really fast"
$i=1$
while $i>0$ :
for character in sentence:
if character == "t":
print("found a ’t’ in sentence")
else:
print("maybe the next character?")

Check back soon!
01:52

Problem 18

Fill in the following table with values True or False—one value in each empty box.
$$
\begin{array}{|c|c|c|c|c|c|}
\hline & & \text { (not } \mathrm{p}) & (\mathrm{p} \text { and } \mathrm{q}) & (\mathrm{p} \text { or } \mathrm{q}) & (\mathrm{p} \text { or } \mathrm{q}) \text { and } \\
\text { p } & \mathrm{q} & \text { or } \mathrm{q} & \text { or } \mathrm{q} & \text { and } \mathrm{p} & (\mathrm{p} \text { and } \mathrm{q}) \\
\text { True } & \text { True } & & & & \\
\text { True } & \text { False } & & & & \\
\text { False } & \text { True } & & & & \\
\text { False } & \text { False } & & & & \\
\hline
\end{array}
$$

Yujie Wang
Yujie Wang
College of San Mateo

Problem 19

If your while loop is controlled by "while True:", it will loop forever. How do you control your loop so that it will stop? Provide a brief example in Python code.

Check back soon!
12:34

Problem 20

The formula that calculates roots for a quadratic equation $a x^2+$ $b x+c$ is the quadratic formula $x=-b \pm \sqrt{b^2-4 a c} / 2 a$. Because the square root of a negative is imaginary, one can use the expression under the square root (known as the discriminant) to check for the type of root. If the discriminant is negative, the roots are imaginary. If the discriminant is zero, there is only one root. If the discriminant is positive, there are two roots.
(a) Write a program that uses the quadratic formula to generate real roots, i.e., ignores the imaginary roots. Use the discriminant to determine whether there is one root or two roots and then print the appropriate answer.
(b) Python uses the letter " $\mathrm{j}$ " to represent the mathematical imaginary number " $\mathrm{i}$ " (a convention used in electrical engineering). However, the Python " $j$ " must always be preceeded by a number. That is, " $1 \mathrm{j}$ " is equivalent to the mathematical " $\mathrm{i}$." Add the ability to handle imaginary roots to your program.

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator

Problem 21

In order to understand what a program tries to accomplish, it is essential to be able to follow the flow of control. In the following example, what happens when $\mathrm{x}=4$ ?
while True:
for $x$ in range (6):
$y=2 * x+1$
print ( $y$ )
if $y>9$ :
break
(a) The program breaks out of the while loop and stops running.
(b) The program breaks out of the for loop, but the while condition continues to be True, resulting in an infinite loop.
(c) The program does not break but simply continues processing the for loop.

Check back soon!
17:41

Problem 22

Create a program that prompts for a positive number greater than 2 (check this condition) and then keeps taking the square root of this number until the square root is less than 2. Print the value each time the square root is taken, along with the number of times the operation has been completed. For example:
Enter an integer greater than 2: 20
1: 4.472
2: 2.115
3: 1.454

Willis James
Willis James
Numerade Educator
03:33

Problem 23

Exercises 33 in the chapter Beginnings gave an algorithm for calculating the pass rating for football quarterbacks. One can use the pass rating to determine whether a quarterback had a mediocre, good, or great year. A rating is poor if it is 85 or below, mediocre if above 85 , good if above 90 , and great if above 95. Add to the pass rating program to output "poor," "mediocre," "good," or "great."
To test your program, you can look up actual data on www.nfl.com or use the following information from 2007:
$$
\begin{array}{lccccc}
& \text { Completions } & \text { Attempts } & \text { Yards } & \text { Touchdowns } & \text { Interceptions } \\
\text { Donovan McNabb } & 180 & 316 & 2647 & 18 & 6 \\
\text { Tom Brady } & 319 & 516 & 3529 & 24 & 12 \\
\text { Peyton Manning } & 362 & 557 & 4397 & 31 & 9
\end{array}
$$

Carson Merrill
Carson Merrill
Numerade Educator
02:02

Problem 24

The NPR radio show Car Talk always offers a puzzle to solve. Occasionally, one can be solved with a program. Try this one: My car's odometer measures distance traveled in whole numbers - no tenths of miles - up to 999,999. Last week I was driving along on the freeway and noticed that the last four digits, but not the last five digits, formed a palindrome; that is, it read the same backward as forward (e.g., 1221). After one mile went by, though, the last five digits did form a palindrome; and, after one more mile, the middle four digits formed a palindrome. Finally, when the third mile rolled over, all six digits formed a palindrome. What was my mileage at the time I first noticed all these palindromes?

Nick Johnson
Nick Johnson
Numerade Educator
01:00

Problem 25

Find the two-digit number such that when you square it, the resulting three-digit number has its rightmost two digits the same as the original two-digit number. That is, for a number in the form $A B, A B^* A B=C A B$ for some $C$.

Aman Gupta
Aman Gupta
Numerade Educator
01:07

Problem 26

A famous puzzle follows.
$$
\begin{array}{r}
\text { SEND } \\
\text { +MORE } \\
\hline \text { MONEY }
\end{array}
$$
Substitute each letter in the equation with a single integer 0-9 (no duplicates) such that the addition is correct. Write a program to solve this puzzle.

Breanna Ollech
Breanna Ollech
Numerade Educator