• Home
  • Textbooks
  • Introduction to Programming Using Python
  • Selections

Introduction to Programming Using Python

Y. Daniel Liang

Chapter 4

Selections - all with Video Answers

Educators

+ 3 more educators

Chapter Questions

12:34

Problem 1

(Algebra: solve quadratic equations) The two roots of a quadratic equation, for example, $a x^{2}+b x+c=0,$ can be obtained using the following formula:
$$r_{1}=\frac{-b+\sqrt{b^{2}-4 a c}}{2 a} \text { and } r_{2}=\frac{-b-\sqrt{b^{2}-4 a c}}{2 a}$$
$b^{2}-4 a c$ is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.
Write a program that prompts the user to enter values for $a, b,$ and $c$ and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is $0,$ display one root. Otherwise, display The equation has no real roots. Here are some sample runs.

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator
09:38

Problem 2

(Game: add three numbers) The program in Listing 4.1 generates two integers and prompts the user to enter the sum of these two integers. Revise the program to generate three single-digit integers and prompt the user to enter the sum of these three integers.

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator
01:50

Problem 3

(Algebra: solve $2 \times 2$ linear equations) You can use Cramer's rule to solve the
following $2 \times 2$ system of linear equation:
\[
\begin{array}{l}
a x+b y=e \\
c x+d y=f
\end{array} \quad x=\frac{e d-b f}{a d-b c} \quad y=\frac{a f-e c}{a d-b c}
\]
Write a program that prompts the user to enter a, b, $c, d, e,$ and $f$ and display the result. If $a d-b c$ is $0,$ report that The equation has no solution.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
07:04

Problem 4

(Game: learn addition) Write a program that generates two integers under 100 and prompts the user to enter the sum of these two integers. The program then reports true if the answer is correct, false otherwise. The program is similar to Listing 4.1

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator
01:59

Problem 5

(Find future dates) Write a program that prompts the user to cnter an integer for today's day of the week (Sunday is 0, Monday is 1, ...., and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week. Here is a sample run:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
13:22

Problem 6

(Health application: $B M I$ ) Revise Listing $4.6,$ ComputeBMI.py, to let users enter their weight in pounds and their height in feet and inches. For example, if a person is 5 feet and 10 inches, you will enter 5 for feet and 10 for inches. Here is a $\operatorname{sam}$ ple run:

LB
Luzan Baral
Numerade Educator
01:47

Problem 7

(Financial application: monetary units) Modify Listing $3.4,$ ComputeChange.py, to display the nonzero denominations only, using singular words for single units such as 1 dollar and 1 penny, and plural words for more than one unit such as 2 dollars and 3 pennies.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:01

Problem 8

(Sort three integers) Write a program that prompts the user to enter three integers and displays them in increasing order.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:05

Problem 9

(Financial: compare costs) Suppose you shop for rice and find it in two differentsized packages. You would like to write a program to compare the costs of the packages. The program prompts the user to enter the weight and price of each package and then displays the one with the better price. Here is a sample run:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:40

Problem 10

(Game: multiplication quiz) Listing 4.4, SubtractionQuiz.py, randomly generates a subtraction question. Revise the program to randomly generate a multiplication question with two integers less than 100 .

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:51

Problem 11

(Find the mumber of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2000 , the program should display that February 2000 has 29 days. If the user entered month 3 and year $2005,$ the program should display that March 2005 has 31 days.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:36

Problem 12

(Check a number) Write a program that prompts the user to enter an integer and checks whether the number is divisible by both 5 and $6,$ divisible by 5 or $6,$ or just one of them (but not both). Here is a sample run:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:34

Problem 13

(Financial application: compute taxes) Listing $4.7,$ ComputeTax.py, gives the source code to compute taxes for single filers. Complete Listing 4.7 to give the complete source code for the other filing statuses.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
05:58

Problem 14

(Game: heads or tails) Write a program that lets the user guess whether a flipped coin displays the head or the tail. The program randomly generates an integer 0 or 1, which represents head or tail. The program prompts the user to enter a guess and reports whether the guess is correct or incorrect.

Bret Rosen
Bret Rosen
Numerade Educator
04:47

Problem 15

(Game: lottery) Revise Listing $4.10,$ Lottery.py, to generate a three-digit lottery number. The program prompts the user to enter a three-digit number and determines whether the user wins according to the following rules:
1. If the user input matches the lottery number in the exact order, the award is $\$ 10,000$
2. If all the digits in the user input match all the digits in the lottery number, the award is $\$ 3,000$
3. If one digit in the user input matches a digit in the lottery number, the award is $\$ 1,000$

Morgan Cheatham
Morgan Cheatham
Numerade Educator
00:41

Problem 16

(Random character) Write a program that displays a random uppercase letter.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:28

Problem 17

(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number $0,1,$ or 2 representing scissor, rock, and paper. The program prompts the user to enter a number $0,1,$ or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:05

Problem 18

(Financials: currency exchange) Write a program that prompts the user to enter the currency exchange rate between U.S. dollars and Chinese Renminbi (RMB). Prompt the user to enter 0 to convert from U.S. dollars to Chinese $\mathrm{RMB}$ and 1 for vice versa. Prompt the user to enter the amount in U.S. dollars or Chinese RMB to convert it to Chinese $\mathrm{RMB}$ or U.S. dollars, respectively. Here are some sample runs:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:27

Problem 19

(Compute the perimeter of a triangle) Write a program that reads three edges for a triangle and computes the perimeter if the input is valid. Otherwise, display that the input is invalid. The input is valid if the sum of every pair of two edges is greater than the remaining edge. Here is a sample run:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:40

Problem 20

(Science: wind-chill temperature) Exercise 2.9 gives a formula to compute the wind-chill temperature. The formula is valid for temperatures in the range between $-58^{\circ} \mathrm{F}$ and $41^{\circ} \mathrm{F}$ and for wind speed greater than or equal to $2 .$ Write a program that prompts the user to enter a temperature and a wind speed. The program displays the wind-chill temperature if the input is valid; otherwise, it displays a message indicating whether the temperature and/or wind speed is invalid.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:17

Problem 21

(Science: day of the week) Zeller's congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is
\[
h=\left(q+\left\lfloor\frac{26(m+1)}{10}\right\rfloor+k+\left\lfloor\frac{k}{4}\right\rfloor+\left\lfloor\frac{j}{4}\right\rfloor+5 j\right) \% 7
\]
where
$\bullet$ $h$ is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, 6: Friday).
$\bullet$ q is the day of the month.
$\bullet$ mis the month (3: March, 4: April, ...., 12: December). January and February are counted as months 13 and 14 of the previous year.
$\bullet$ j is the century (i.e.. $\left|\frac{\text { year }}{100}\right|$ ).
$\bullet$ k is the year of the century (i.e., year $\% 100$ ).
Write a program that prompts the user to enter a year, month, and day of the month, and then it displays the name of the day of the week. Here are some sample runs:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:43

Problem 22

(Geometry: point in a circle?) Write a program that prompts the user to enter a point $(x, y)$ and checks whether the point is within the circle centered at (0,0) with radius $10 .$ For example, (4,5) is inside the circle and (9,9) is outside the circle, as shown in Figure 4.8 a.
Figure 4.8
(a) Points inside and outside of the circle:
(b) points inside and outside of the rectangle.
(Hint: A point is in the circle if its distance to (0,0) is less than or equal to 10 . The formula for computing the distance is $\sqrt{\left(x_{2}-x_{1}\right)^{2}+\left(y_{2}-y_{1}\right)^{2}}$. Test your program to cover all cases.) Two sample runs are shown next.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:27

Problem 23

(Geometry: point in a rectangle?) Write a program that prompts the user to enter a point $(x, y)$ and checks whether the point is within the rectangle centered at (0,0) with width 10 and height $5 .$ For example, (2,2) is inside the rectangle and (6,4) is outside the rectangle, as shown in Figure $4.8 b$. (Hint: A point is in the rectangle if its horizontal distance to (0,0) is less than or equal to $10 / 2$ and its vertical distance to (0,0) is less than or equal to $5.0 / 2 .$ Test your program to cover all cases.) Here are two sample runs:

Akash M
Akash M
Numerade Educator
01:12

Problem 24

(Game: pick a card) Write a program that simulates picking a card from a deck of 52 cards. Your program should display the rank (Ace, $2,3,4,5,6,7,8,9,10,$ Jack, Queen, King) and suit (CTubs, Diamonds, Hearts, Spades) of the card. Here is a sample run of the program:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:41

Problem 25

(Geometry: intersecting point) Two points on line 1 are given as $(x 1, y 1)$ and $(x 2,$
$\left.y^{2}\right)$ and on line 2 as $\left(x 3, y^{3}\right)$ and $(x 4, y 4),$ as shown in Figure $4.9 a-b$
Two lines intersect in $(a-b)$ and two lines are parallel in (c).
The intersecting point of the two lines can be found by solving the following linear equation:
$$\begin{array}{l}
\left(y_{1}-y_{2}\right) x-\left(x_{1}-x_{2}\right) y=\left(y_{1}-y_{2}\right) x_{1}-\left(x_{1}-x_{2}\right) y_{1} \\
\left(y_{3}-y_{4}\right) x-\left(x_{3}-x_{4}\right) y=\left(y_{3}-y_{4}\right) x_{3}-\left(x_{3}-x_{4}\right) y_{3}
\end{array}$$
This linear equation can be solved using Cramer's rule (see Exercise 4.3 ). If the equation has no solutions, the two lines are parallel (Figure $4.9 \mathrm{c}$ ). Write a program that prompts the user to cnter four points and displays the intersecting point. Here are sample runs:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:41

Problem 26

(Palindrome number) Write a program that prompts the user to enter a three-digit integer and determines whether it is a palindrome number. A number is a palindrome if it reads the same from right to left and from left to right. Here is a sample run of this program:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:53

Problem 27

(Geometry: points in triangle?) Suppose a right triangle is placed in a plane as shown below. The right-angle point is at $(0,0),$ and the other two points are at $(200,0),$ and $(0,100) .$ Write a program that prompts the user to enter a point with $x-$ and $y-$ coordinates and determines whether the point is inside the triangle. Here are some sample runs:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:59

Problem 28

(Geometry: two rectangles) Write a program that prompts the user to enter the center $x=, y$ -coordinates, width, and height of two rectangles and determines whether the second rectangle is inside the first or overlaps with the first, as shown in Figure $4.10 .$ Test your program to cover all cases.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:03

Problem 29

(Geometry: two circles) Write a program that prompts the user to enter the center coordinates and radii of two circles and determines whether the second circle is inside the first or overlaps with the first, as shown in Figure 4.11 . (Hint: circle 2 is inside circle 1 if the distance between the two centers $<=|r 1-r 2|$ and circle 2 overlaps circle if the distance between the two centers $<\mathrm{r} 1+\mathrm{r} 2 .$ Test your program to cover all cases.)

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:21

Problem 30

(Current time) Revise Programming Exercise 2.18 to display the hour using a 12 hour clock. Here is a sample run:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:43

Problem 31

(Geometry: point position) Given a directed line from point $\mathrm{p} 0(\mathrm{x} 0, \mathrm{y} 0)$ to $\mathrm{p} 1(\mathrm{x} 1$ y 1 ), you can use the following condition to decide whether a point $\mathrm{p} 2(\mathrm{x} 2, \mathrm{y} 2)$ is on the left side of the line, on the right side of the line, or on the same line (see Figure 4.12 ):
$(x 1-x 0)^{*}(y 2-y 0)-(x 2-x 0)^{*}(y 1-y 0)\left\{\begin{array}{ll}>0 & p 2 \text { is on the left side of the line } \\ =0 & p 2 \text { is on the same line } \\ <0 & p 2 \text { is on the right side of the line }\end{array}\right.$
(a) $\mathrm{p} 2$ is on the left side of the line.
(b) $\mathrm{p} 2$ is on the right side of the line.
(c) $\mathrm{p} 2$ is on the same line.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:48

Problem 32

(Geometry: point on line segment) Exercise 4.31 shows how to test whether a point is on an unbounded line. Revise Exercise 4.31 to test whether a point is on a line segment. Write a program that prompts the user to enter the $x$ - and $y$ -coordinates for the three points $\mathrm{p} 0, \mathrm{p} 1,$ and $\mathrm{p} 2$ and displays whether $\mathrm{p} 2$ is on the line segment from p0 to p1. Here are some sample runs:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:05

Problem 33

(Decimal to hex) Write a program that prompts the user to enter an integer between 0 and 15 and displays its corresponding hex number, Here are some sample runs:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:31

Problem 34

(Hex to decimal) Write a program that prompts the user to enter a hex character and displays its corresponding decimal integer. Here are some sample runs:

Morgan Cheatham
Morgan Cheatham
Numerade Educator
05:17

Problem 35

(Turtle: point position) Write a program that prompts the user to enter the $x$ - and y-coordinates for the three points $\mathrm{p} 0, \mathrm{p} 1,$ and $\mathrm{p} 2,$ and displays a message to indicate whether $\mathrm{p} 2$ is on the left side, the right side, or on the line from p0 to $\mathrm{pl}$, as shown in Figure $4.13 .$ See Exercise 4.31 for determining the point position.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:32

Problem 36

(Turtle: point in a circle?) Modify Listing 4.11 to let the program randomly generate a point within the square whose center is the same as the circle center and whose side is the diameter of the circle. Draw the circle and the point. Display a message to indicate whether the point is inside the circle.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:30

Problem 37

(Turtle: point in a rectangle?) Write a program that prompts the user to enter a point $(x, y)$ and checks whether the point is within the rectangle centered at $(0,$ 0) with width 100 and height $50 .$ Display the point, the rectangle, and a message indicating whether the point is inside the rectangle in the window, as shown in Figure 4.14

Morgan Cheatham
Morgan Cheatham
Numerade Educator
04:13

Problem 38

(Geometry: two rectangles) Write a program that prompts the user to enter the center $x$ -, y-coordinates, width, and height of two rectangles and determines whether the second rectangle is inside the first or overlaps with the first, as shown in Figure 4.15

Morgan Cheatham
Morgan Cheatham
Numerade Educator
04:50

Problem 39

(Turtle: two circles) Write a program that prompts the user to enter the center coordinates and radii of two circles and determines whether the second circle is inside the first or overlaps with the first, as shown in Figure 4.16
The program displays two circles and a status message.

Morgan Cheatham
Morgan Cheatham
Numerade Educator