0% found this document useful (0 votes)
96 views5 pages

Ejercicios de Programación 03

1. The document discusses various Python programming exercises related to math, strings, and objects. 2. It includes exercises on computing areas of shapes like pentagons and polygons, distances between geographic points, character encoding, and financial applications. 3. Sample programs prompt the user for input values, perform calculations, and display outputs like areas, distances, or payroll statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views5 pages

Ejercicios de Programación 03

1. The document discusses various Python programming exercises related to math, strings, and objects. 2. It includes exercises on computing areas of shapes like pentagons and polygons, distances between geographic points, character encoding, and financial applications. 3. Sample programs prompt the user for input values, perform calculations, and display outputs like areas, distances, or payroll statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Exercises 85

3.30 How do you set the turtle’s color?


3.31 How do you fill a shape with color?
3.32 How do you make the turtle invisible?
✓ Check
Point

KEY TERMS
backslash (\) 69 methods 75
character encoding 68 newline 69
end-of-line 69 object 74
escape sequence 69 string 67
line break 69 whitespace characters 76

CHAPTER SUMMARY
1. Python provides the mathematical functions abs, max, min, pow, and round in the
interpreter and the functions fabs, ceil, floor, exp, log, sqrt, sin, asin, cos,
acos, tan, degrees, and radians in the math module.

2. A string is a sequence of characters. String values can be enclosed in matching single


quotes (') or double quotes ("). Python does not have a data type for characters; a
single-character string represents a character.

3. An escape sequence is a special syntax that begins with the character \ followed by a
letter or a combination of digits to represent special characters, such as \', \", \t,
and \n.

4. The characters ' ', \t , \f, \r, and \n are known as the whitespace characters.

5. All data including numbers and strings are objects in Python. You can invoke
methods to perform operations on the objects.

6. You can use the format function to format a number or a string and return the result
as a string.

TEST QUESTIONS
Do test questions for this chapter online at www.cs.armstrong.edu/liang/py/test.html.

PROGRAMMING EXERCISES
Section 3.2
3.1 (Geometry: area of a pentagon) Write a program that prompts the user to enter the
length from the center of a pentagon to a vertex and computes the area of the pen-
tagon, as shown in the following figure.

r
86 Chapter 3 Mathematical Functions, Strings, and Objects
3 23 2
The formula for computing the area of a pentagon is Area = s , where s is
2 p
the length of a side. The side can be computed using the formula s = 2r sin ,
5
where r is the length from the center of a pentagon to a vertex. Here is a sample
run:

Enter the length from the center to a vertex: The area of the 5.5
pentagon is 108.61

*3.2 (Geometry: great circle distance) The great circle distance is the distance between
two points on the surface of a sphere. Let (x1, y1) and (x2, y2) be the
geographical latitude and longitude of two points. The great circle distance
between the two points can be computed using the following formula:

d = radius * arccos(sin(x 1) * sin(x 2) + cos(x 1) * cos(x 2) * cos(y1 -


y2))

Write a program that prompts the user to enter the latitude and longitude of two
points on the earth in degrees and displays its great circle distance. The average
earth radius is 6,371.01 km. Note that you need to convert the degrees into
radians using the math.radians function since the Python trigonometric
functions use radians. The latitude and longitude degrees in the formula are for
north and west. Use negative to indicate south and east degrees. Here is a sample
run:

Enter point 1 (latitude and longitude) in degrees:


39.55, -116.25

Enter point 2 (latitude and longitude) in degrees:


41.5, 87.37
The distance between the two points is 10691.79183231593 km

*3.3 (Geography: estimate areas) Find the GPS locations for Atlanta, Georgia;
Orlando, Florida; Savannah, Georgia; and Charlotte, North Carolina from
www.gps-data-team.com/map/ and compute the estimated area enclosed by these
four cities. (Hint: Use the formula in Programming Exercise 3.2 to compute the
distance between two cities. Divide the polygon into two triangles and use the
for- mula in Programming Exercise 2.14 to compute the area of a triangle.)
3.4 (Geometry: area of a pentagon) The area of a pentagon can be computed using
the following formula (s is the length of a side):
5 * s2
Area =
p
4 * tan ¢ ≤
5
Write a program that prompts the user to enter the side of a pentagon and displays
the area. Here is a sample run:

Enter the side: 5.5


The area of the pentagon is 53.04444136781625
Programming Exercises 87
*3.5 (Geometry: area of a regular polygon) A regular polygon is an n-sided polygon
in which all sides are of the same length and all angles have the same degree (i.e.,
the polygon is both equilateral and equiangular). The formula for computing the
area of a regular polygon is
n * s2
Area =
p
4 * tan ¢ ≤
n
Here, s is the length of a side. Write a program that prompts the user to enter the
number of sides and their length of a regular polygon and displays its area. Here
is a sample run:

Enter the number of sides: 5


Enter the side: 6.5
The area of the polygon is 73.69017017488385

Sections 3.3–3.6
*3.6 (Find the character of an ASCII code) Write a program that receives an ASCII
code (an integer between 0 and 127) and displays its character. For example, if
the user enters 97, the program displays the character a. Here is a sample run:

Enter an ASCII code: 69


The character is E

3.7 (Random character) Write a program that displays a random uppercase letter
using the time.time() function.
*3.8 (Financial application: monetary units) Rewrite Listing 3.4, ComputeChange.py,
to fix the possible loss of accuracy when converting a float value to an int value.
Enter the input as an integer whose last two digits represent the cents. For exam-
ple, the input 1156 represents 11 dollars and 56 cents.
*3.9 (Financial application: payroll) Write a program that reads the following infor-
mation and prints a payroll statement:
Employee’s name (e.g., Smith)
Number of hours worked in a week (e.g., 10)
Hourly pay rate (e.g., 9.75)
Federal tax withholding rate (e.g., 20%)
State tax withholding rate (e.g., 9%)
A sample run is shown below:
Enter employee's name: Smith
Enter number of hours worked in a week: 10
Enter hourly pay rate: 9.75
Enter federal tax withholding rate: 0.20
Enter state tax withholding rate: Employee 0.09

Name: Smith
88 Chapter 3 Mathematical Functions, Strings, and Objects

Hours Worked: 10.0


Pay Rate: $9.75
Gross Pay: $97.5
Deductions:
Federal Withholding (20.0%): $19.5
State Withholding (9.0%): $8.77
Total Deduction: $28.27
Net Pay: $69.22

*3.10 (Turtle: display Unicodes) Write a program to display Greek letters abgdPzhu.
The Unicode of these characters are \u03b1 \u03b2 \u03b3 \u03b4 \u03b5
\u03b6 \u03b7 \u03b8.
3.11 (Reverse number) Write a program that prompts the user to enter a four-digit inte-
ger and displays the number in reverse order. Here is a sample run:

Enter an integer: 3125


The reversed number is 5213

Sections 3.7–3.8
**3.12 (Turtle: draw a star) Write a program that prompts the user to enter the length of the
star and draw a star, as shown in Figure 3.5a. (Hint: The inner angle of each
point in the star is 36 degrees.)

(a) (b) (c)


FIGURE 3.5 The program (a) draws a star, (b) displays a STOP sign, and (c) draws an Olympic symbol.

*3.13 (Turtle: display a STOP sign) Write a program that displays a STOP sign, as shown
in Figure 3.5b. The hexagon is in red and the text is in white.
3.14 (Turtle: draw the Olympic symbol ) Write a program that prompts the user to
enter the radius of the rings and draws an Olympic symbol of five rings of the
same size with the colors blue, black, red, yellow, and green, as shown in
Figure 3.5c.
*3.15 (Turtle: paint a smiley face) Write a program that paints a smiley face, as shown in
Figure 3.6a.
Programming Exercises 89

(a) (b)
FIGURE 3.6 The program paints a smiley face in (a) and draws five shapes with bottom edges parallel to the x-axis in (b).

**3.16 (Turtle: draw shapes) Write a program that draws a triangle, square, pentagon,
hexagon, and octagon, as shown in Figure 3.6b. Note that the bottom edges of
these shapes are parallel to the x-axis. (Hint: For a triangle with a bottom line
parallel to the x-axis, set the turtle’s heading to 60 degrees.)
*3.17 (Turtle: triangle area) Write a program that prompts the user to enter the three
points p1, p2, and p3 for a triangle and display its area below the triangle, as
shown in Figure 3.7a. The formula for computing the area of a triangle is given
in Exercise 2.14.

(a) (b) (c)

FIGURE 3.7 The program displays (a) the area of the triangle and (b) the angles for the triangle. (c) The program draws a
line.

*3.18 (Turtle: triangle angles) Revise Listing 3.2, ComputeAngles.py, to write a pro- gram
that prompts the user to enter the three points p1, p2, and p3 for a triangle and
display its angles, as shown in Figure 3.7b.
**3.19 (Turtle: draw a line) Write a program that prompts the user to enter two points and
draw a line to connect the points and displays the coordinates of the points, as
shown in Figure 3.7c.

You might also like