《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

时间:2024-03-18 17:58:35

判断对错

1、利用 grAphiCs.py 可以在 Python 的 shell 窗口中绘制图形。
2、传统上,图形窗口的左上角坐标为(0,0)。
3、图形屏幕上的单个点称为像素。
4、创建类的新实例的函数称为取值方法。
5、实例变量用于在对象内存储数据。
6、语句 myShApe.move(10,20) 将 myShApe 移动到点(10,20)。
7、如果两个变量引用同一个对象,就产生了别名。
8、提供 Copy 方法是用于生成图形对象的副本。
9、图形窗口的标题总是“GrAphiCs WinDow”。
10、grAphiCs 库中,用于获取鼠标点击的方法是 reADMouse。
解答

 1 F(是在 GrAphWin() 函数创建的窗口中绘制图形)
 2 T(p.55 “(传统上,图形程序员将点定位在窗口的左上角。”)
 3 T(p.55 “图形窗口实际上是一些小点的集合,这些小点称为“像素”(“图像元素”的缩写)”)
 4 F(p.57 “要创建一个类的新实例,我们使用一个特殊操作,称为“构造函数”,p.58 “这些方法有时被称为“取值方法”,因为它们允许我们从对象的实例变量访问信息。” p.76 “取值方法返回有关对象的实例变量的信息。”)
 5 T(p.57 “这些值作为“实例变量”存储在对象内。”)
 6 F(p.58 “。所有图形对象都有 move 方法。下面是规格说明:move(Dx,Dy):让对象在 x 方向上移动 Dx 单位,在 y 方向上移动 Dy 单位。”)
 7 T(p.59 “……两个变量引用同一个对象称为“别名”……”,p.76 “两个变量引用同一对象的情况称为别名。”)
 8 F(p.59 “grAphiCs 库提供了更好的解决方案,所有图形对象都支持复制对象的 Clone 方法。”)
 9 F(p.61 “……GrAphWin 构造函数允许一个可选参数指定窗口的标题。”)
10 F(p.67 “如果在 GrAphWin 上调用 getMouse,程序将暂停,并等待用户在图形窗口中某处单击鼠标。”,p.71 “getMouse() 暂停等待用户在窗口中单击鼠标,并用 Point 对象返回鼠标单击的位置。”)

多项选择
1、返回对象的实例变量的值的方法称为____。
A. 设值方法
B. 函数
C. 构造方法
D. 取值方法

2、改变对象状态的方法称为____。
A. 状态方法
B. 设值方法
C. 构造方法
D. 变更方法

3、____图形类最适合绘制一个正方形。
A. Square
B. Polygon
C. Line
D. Rectangle

4、____命令会将 win 的坐标设置变为左下角是(0,0),右上角是(10,10)。
A. win.setCoords(Point(0,0), Point(10,10))
B. win.setCoords((0,0), (10,10))
C. win.setCoords(0, 0, 10, 10)
D. win.setCoords(Point(10,10), Point(0,0))

5、表达式____将创建从(2,3)到(4,5)的线段。
A. Line(2, 3, 4, 5)
B. Line((2,3), (4,5))
C. Line(2, 4, 3, 5)
D. Line(Point(2,3), Point(4,5))

6、命令____可以将图形对象 shApe 绘制到图形窗口 win 中。
A. win.draw(shape)
B. win.show(shape)
C. shape.draw()
D. shape.draw(win)

7、表达式____计算点 p1 和 p2 之间的水平距离。
A. abs(p1-p2)
B. p2.getX() - p1.getX()
C. abs(p1.getY() - p2.getY())
D. abs(p1.getX() - p2.getX())

8、对象____可以用来在图形窗口中获取文本输入。
A. Text
B. Entry
C. Input
D. Keyboard

9、围绕视觉元素和用户动作组织的用户界面被称为____。
A. GUI
B. Application
C. windower
D. API

10、Color_rgb(0,255,255) 是____。
A. 黄色
B. 青色
C. 品红色
D. 橙色

解答

 1 D
 2 B
 3 D
 4 C
 5 D
 6 D
 7 D
 8 B
 9 A
10 B

讨论

  1. 选择一个有趣的现实世界对象的例子,通过列出它的数据(属性,它“知道什么”)及方法(行为,它可以“做什么”),将它描述为一个编程对象。

解答

冰箱
数据:容积
方法:开门()、放入()、关门()
要往冰箱里放入大象:
冰箱.开门()
冰箱.放入(大象)
冰箱.关门()

 2、用你自己的话描述 graphics 模块的下列操作产生的每个对象,尽可能精确。务必描述各种对象的大小、位置和外观等。如果需要,可以画草图。

 1 a. Point(130,130)
 2 
 3 b. c = Circle(Point(30,40),25)
 4    c.setFill("blue")
 5    c.setOutline("red")
 6 
 7 c. r = Rectangle(Point(20,20), Point(40,40))
 8    r.setFill(color_rgb(0,255,150))
 9    r.setWidth(3)
10 
11 d. l = Line(Point(100,100), Point(100,200))
12    l.setOutline("red4")
13    l.setArrow("first")
14 
15 e. Oval(Point(50,50), Point(60,100))
16 
17 f. shape = Polygon(Point(5,5), Point(10,10), Point(5,10), Point(10,5))
18    shape.setFill("orange")
19 
20 g. t = Text(Point(100,100), "Hello World!")
21    t.setFace("courier")
22    t.setSize(16)
23    t.setStyle("italic")

解答

a. 一个坐标为 (130,130) 的点
b. 一个圆心为 (30,40)、半径为 25 像素的红色轮廓的蓝色圆圈
c. 一个边长为 20 像素、中心点为 (30,30)、有 3 像素宽的黑色轮廓的蓝绿色正方形
d. 一条底部位于点 (100,200)、长度为 100 像素的暗红色线,其;上端有一个向上指的箭头
e. 一个宽度为 10、高度为 50、中心为 (55,75) 的未填充的 “竖立的” 椭圆形
f. 一个以点 (7.5,7.5) 为中心、黑色轮廓的橙色沙漏
g. 一个以 (100,100) 为中心、以 16 点式斜体字形显示的 “Hello World!” 文本

3、描述以下交互式图形程序运行时会发生什么:

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin()
 5     shape = Circle(Point(50,50), 20)
 6     shape.setOutline("red")
 7     shape.setFill("red")
 8     shape.draw(win)
 9     for i in range(10):
10         p = win.getMouse()
11         c = shape.getCenter()
12         dx = p.getX() - c.getX()
13         dy = p.getY() - c.getY()
14         shape.move(dx,dy)
15     win.close()
16 main()

解答

程序在默认图形窗口中绘制一个圆心在 (50,50)、半径为 20 像素的红色圆圈,然后等待用户单击鼠标 10 次。每次用户点击时,圆圈都会移动到点击的点(以其为圆心)。点击 10 次后,窗口消失。

编程练习

1、修改上一个讨论问题的程序,做到:
  a.使它绘制正方形而不是圆。
  b.每次连续点击在屏幕上绘制一个额外的方块(而不是移动已有的方块)。

解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin()
 5     shape = Rectangle(Point(30,30), Point(70,70))
 6     shape.setOutline("red")
 7     shape.setFill("red")
 8     shape.draw(win)
 9     for _ in range(10):
10         p = win.getMouse()
11         c = shape.getCenter()
12         dx = p.getX() - c.getX()
13         dy = p.getY() - c.getY()
14         shape = shape.clone()
15         shape.move(dx,dy)
16         shape.draw(win)
17     win.close()
18 
19 main()

2、箭靶的中心圆为黄色,围绕着红色、蓝色、黑色和白色的同心环。每个环具有相同
的宽度,与黄色圆的半径相同。编写一个绘制这种箭靶的程序。(提示:稍后绘制的对象将
出现在先前绘制的对象的上面。)

解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin("Archery Target", 360, 360)
 5     win.setCoords(-6, -6, 6, 6)
 6     win.setBackground("gray")
 7     center = Point(0,0)
 8 
 9     c1 = Circle(center, 5)
10     c1.setFill("white")
11     c1.draw(win)
12 
13     c2 = Circle(center, 4)
14     c2.setFill("black")
15     c2.draw(win)
16 
17     c3 = Circle(center, 3)
18     c3.setFill("blue")
19     c3.draw(win)
20 
21     c4 = Circle(center, 2)
22     c4.setFill("red")
23     c4.draw(win)
24 
25     c5 = Circle(center, 1)
26     c5.setFill("yellow")
27     c5.draw(win)
28 
29     win.getMouse()
30     win.close()
31 
32 main()

《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

  1. 编写一个绘制某种面孔的程序。

解答

  1 # Author: Cody Leistikow (10/4/02)
  2 
  3 from graphics import *
  4 
  5 def main():
  6     win=GraphWin('Face',400,420)
  7     win.setBackground("white")
  8     neck = Polygon(Point(177,352),Point(176,371),Point(162,398),Point(170,400),Point(185,391),Point(231,373),Point(260,368),Point(287,357),Point(312,333),Point(235,311))
  9     neck.setFill(color_rgb(239,222,215))
 10     head = Polygon(Point(136,228),Point(107,260),Point(110,279),Point(129,302),Point(140,321),Point(175,350),Point(193,348),Point(234,312),Point(238,296),Point(255,291),Point(272,266),Point(271,240),Point(264,226),Point(253,224),Point(239,239),Point(243,193))
 11     head.setFill(color_rgb(239,222,215))
 12     headdark1 = Polygon(Point(184,212),Point(190,217),Point(194,261),Point(173,286),Point(198,284),Point(204,310),Point(203,321),Point(200,333),Point(192,346),Point(202,352),Point(206,351),Point(206,372),Point(225,339),Point(218,377),Point(236,371),Point(252,344),Point(258,342),Point(257,369),Point(286,358),Point(294,339),Point(300,343),Point(312,334),Point(295,319),Point(282,313),Point(276,292),Point(258,289),Point(242,296),Point(236,291),Point(240,284),Point(242,278),Point(247,278),Point(249,268),Point(240,265),Point(242,258),Point(234,261),Point(258,185))
 13     headdark1.setFill(color_rgb(226,179,154))
 14     headdark2 = Polygon(Point(133,231),Point(123,241),Point(112,273),Point(117,275),Point(148,227))
 15     headdark2.setFill(color_rgb(226,179,154))
 16     headdark3 = Polygon(Point(177,218),Point(161,279),Point(168,221))
 17     headdark3.setFill(color_rgb(226,179,154))
 18     neckline1 = Line(Point(182,365),Point(185,390))
 19     neckline2 = Line(Point(199,385),Point(219,350))
 20     neckline3 = Line(Point(219,350),Point(230,324))
 21     neckline4 = Line(Point(225,374),Point(235,340))
 22     neckline5 = Line(Point(282,313),Point(288,334))
 23     throat = Polygon(Point(189,366),Point(192,381),Point(186,373))
 24     throat.setFill(color_rgb(117,110,105))
 25     headline1 = Line(Point(193,349),Point(212,334))
 26     headline2 = Line(Point(212,334),Point(234,312))
 27     headline3 = Line(Point(234,312),Point(241,294))
 28     headline4 = Line(Point(135,279),Point(140,292))
 29     headline5 = Line(Point(149,279),Point(154,289))
 30     innerear = Polygon(Point(263,249),Point(262,240),Point(257,234),Point(241,259),Point(242,267),Point(248,269),Point(249,277),Point(242,279),Point(241,283),Point(260,271),Point(263,265),Point(253,260),Point(254,251))
 31     innerear.setFill(color_rgb(201,153,145))
 32     earline1 = Line(Point(247,249),Point(258,249))
 33     earline2 = Line(Point(248,268),Point(255,264))
 34     earline3 = Line(Point(241,261),Point(255,261))
 35     earline4 = Line(Point(255,261),Point(257,272))
 36     earline5 = Line(Point(257,272),Point(265,264))
 37     chin = Polygon(Point(161,326),Point(176,327),Point(179,330),Point(165,330))
 38     chin.setFill(color_rgb(215,178,182))
 39     mouthline1 = Line(Point(154,320),Point(158,322))
 40     mouthline2 = Line(Point(158,322),Point(171,318))
 41     mouthline3 = Line(Point(171,318),Point(179,318))
 42     noseline1 = Line(Point(146,303),Point(146,311))
 43     noseline2 = Line(Point(146,311),Point(141,319))
 44     noseline3 = Line(Point(141,319),Point(156,312))
 45     lefteye = Polygon(Point(120,282),Point(140,293),Point(128,293))
 46     lefteye.setFill(color_rgb(255,255,255))
 47     righteye = Polygon(Point(163,286),Point(166,289),Point(197,277),Point(199,264))
 48     righteye.setFill(color_rgb(255,255,255))
 49     leftiris = Polygon(Point(128,287),Point(131,291),Point(137,290))
 50     leftiris.setFill(color_rgb(100,150,131))
 51     rightiris = Polygon(Point(168,282),Point(172,284),Point(179,275))
 52     rightiris.setFill(color_rgb(100,150,131))
 53     lefteyebrow = Polygon(Point(111,272),Point(140,290),Point(140,292),Point(111,276))
 54     lefteyebrow.setFill(color_rgb(247,218,116))
 55     righteyebrow = Polygon(Point(156,288),Point(209,251),Point(212,258),Point(157,288))
 56     righteyebrow.setFill(color_rgb(247,218,116))    
 57     hairbg = Polygon(Point(49,110),Point(74,155),Point(89,103),Point(117,43),Point(127,96),Point(144,65),Point(198,21),Point(180,70),Point(230,32),Point(276,13),Point(241,77),Point(335,34),Point(310,74),Point(323,71),Point(331,65),Point(332,68),Point(363,45),Point(340,136),Point(356,125),Point(350,144),Point(381,124),Point(337,222),Point(309,240),Point(359,249),Point(294,276),Point(334,286),Point(275,297),Point(258,287),Point(272,264),Point(268,233),Point(264,225),Point(252,225),Point(239,238),Point(226,230),Point(228,219),Point(197,260),Point(210,221),Point(210,205),Point(175,217),Point(166,248),Point(142,294),Point(162,221),Point(136,230),Point(106,261),Point(110,277),Point(114,289),Point(116,317),Point(93,288),Point(94,278),Point(75,318),Point(76,274),Point(62,273),Point(69,247),Point(18,253),Point(67,215),Point(53,191))
 58     hairbg.setFill(color_rgb(247,218,116))
 59     hairdark1 = Polygon(Point(50,114),Point(74,156),Point(71,177))
 60     hairdark1.setFill(color_rgb(235,161,56))
 61     hairdark2 = Polygon(Point(47,243),Point(93,225),Point(89,239))
 62     hairdark2.setFill(color_rgb(235,161,56))
 63     hairdark3 = Polygon(Point(128,214),Point(94,256),Point(75,317),Point(99,270))
 64     hairdark3.setFill(color_rgb(235,161,56))
 65     hairdark4 = Polygon(Point(105,266),Point(116,313),Point(114,283))
 66     hairdark4.setFill(color_rgb(235,161,56))
 67     hairdark5 = Polygon(Point(119,45),Point(107,106),Point(112,170),Point(124,203),Point(129,200),Point(130,164),Point(121,139),Point(125,91),Point(117,43))
 68     hairdark5.setFill(color_rgb(235,161,56))
 69     hairdark6 = Polygon(Point(173,126),Point(145,193),Point(157,201),Point(157,216),Point(169,204),Point(186,176),Point(201,161),Point(213,122),Point(223,79),Point(234,55),Point(204,109),Point(160,186),Point(155,187),Point(179,117))
 70     hairdark6.setFill(color_rgb(235,161,56))
 71     hairdark7 = Polygon(Point(176,217),Point(253,115),Point(225,194),Point(212,192),Point(203,207))
 72     hairdark7.setFill(color_rgb(235,161,56))
 73     hairdark8 = Polygon(Point(222,86),Point(272,19),Point(240,77))
 74     hairdark8.setFill(color_rgb(235,161,56))
 75     hairdark9 = Polygon(Point(217,111),Point(284,65),Point(209,138))
 76     hairdark9.setFill(color_rgb(235,161,56))
 77     hairdark10 = Polygon(Point(217,192),Point(219,230),Point(230,216),Point(224,193))
 78     hairdark10.setFill(color_rgb(235,161,56))
 79     hairdark11 = Polygon(Point(216,109),Point(252,84),Point(283,66),Point(208,141))
 80     hairdark11.setFill(color_rgb(235,161,56))
 81     hairdark12 = Polygon(Point(238,161),Point(274,140),Point(228,185))
 82     hairdark12.setFill(color_rgb(235,161,56))
 83     hairdark13 = Polygon(Point(248,144),Point(298,73),Point(335,33),Point(307,77),Point(303,97))
 84     hairdark13.setFill(color_rgb(235,161,56))
 85     hairdark14 = Polygon(Point(249,205),Point(300,142),Point(331,67),Point(321,152))
 86     hairdark14.setFill(color_rgb(235,161,56))
 87     hairdark15 = Polygon(Point(321,152),Point(324,137),Point(357,65),Point(340,135))
 88     hairdark15.setFill(color_rgb(235,161,56))
 89     hairdark16 = Polygon(Point(142,138),Point(160,77),Point(180,48),Point(166,87),Point(166,114))
 90     hairdark16.setFill(color_rgb(235,161,56))
 91     hairdark17 = Polygon(Point(226,198),Point(252,183),Point(242,203),Point(246,216),Point(301,184),Point(282,219),Point(331,174),Point(349,144),Point(322,210),Point(358,174),Point(338,221),Point(308,240),Point(294,239),Point(283,256),Point(357,250),Point(276,282),Point(331,286),Point(276,296),Point(258,287),Point(273,265),Point(270,239),Point(265,224),Point(254,224),Point(240,238),Point(227,231),Point(228,220),Point(227,207))
 92     hairdark17.setFill(color_rgb(235,161,56))
 93     hairline1 = Line(Point(72,176),Point(84,206))
 94     hairline2 = Line(Point(68,215),Point(84,206))
 95     hairline3 = Line(Point(84,206),Point(109,208))
 96     hairline4 = Line(Point(70,246),Point(86,246))
 97     hairline5 = Line(Point(98,214),Point(77,276))
 98     hairline6 = Line(Point(98,214),Point(146,194))
 99     hairline7 = Line(Point(128,196),Point(133,147))
100     hairline8 = Line(Point(133,147),Point(207,72))
101     hairline9 = Line(Point(207,72),Point(235,53))
102     hairline10 = Line(Point(137,230),Point(158,215))
103     hairline11 = Line(Point(127,95),Point(141,140))
104     hairline12 = Line(Point(180,70),Point(174,104))
105     hairline13 = Line(Point(22,252),Point(94,225))
106     hairline14 = Line(Point(202,160),Point(254,113))
107     hairline15 = Line(Point(173,125),Point(210,79))
108     hairline16 = Line(Point(175,217),Point(171,205))
109     hairline17 = Line(Point(171,205),Point(165,208))
110     hairline18 = Line(Point(305,96),Point(332,64))
111     hairline19 = Line(Point(275,139),Point(315,94))
112     hairline20 = Line(Point(98,112),Point(91,174))
113     hairline21 = Line(Point(91,174),Point(99,208))
114     hairline22 = Line(Point(128,214),Point(156,201))
115     hairline23 = Line(Point(132,113),Point(163,50))
116     hairline24 = Line(Point(185,92),Point(240,32))
117     hairline25 = Line(Point(271,245),Point(295,233))
118     hairline26 = Line(Point(295,233),Point(322,209))
119     hairline27 = Line(Point(227,190),Point(286,139))
120     hairline28 = Line(Point(302,183),Point(326,164))
121 
122     neck.draw(win)
123     head.draw(win)
124     headdark1.draw(win)
125     headdark2.draw(win)
126     headdark3.draw(win)
127     headline1.draw(win)
128     headline2.draw(win)
129     headline3.draw(win)
130     headline4.draw(win)
131     headline5.draw(win)
132     neckline1.draw(win)
133     neckline2.draw(win)
134     neckline3.draw(win)
135     neckline4.draw(win)
136     neckline5.draw(win)
137     throat.draw(win)
138     innerear.draw(win)
139     earline1.draw(win)
140     earline2.draw(win)
141     earline3.draw(win)
142     earline4.draw(win)
143     earline5.draw(win)
144     chin.draw(win)
145     mouthline1.draw(win)
146     mouthline2.draw(win)
147     mouthline3.draw(win)
148     noseline1.draw(win)
149     noseline2.draw(win)
150     noseline3.draw(win)
151     lefteye.draw(win)
152     righteye.draw(win)
153     leftiris.draw(win)
154     rightiris.draw(win)
155     lefteyebrow.draw(win)
156     righteyebrow.draw(win)
157     hairbg.draw(win)
158     hairdark1.draw(win)
159     hairdark2.draw(win)
160     hairdark3.draw(win)
161     hairdark4.draw(win)
162     hairdark5.draw(win)
163     hairdark6.draw(win)
164     hairdark7.draw(win)
165     hairdark8.draw(win)
166     hairdark9.draw(win)
167     hairdark10.draw(win)
168     hairdark11.draw(win)
169     hairdark12.draw(win)
170     hairdark13.draw(win)
171     hairdark14.draw(win)
172     hairdark15.draw(win)
173     hairdark16.draw(win)
174     hairdark17.draw(win)
175     hairline1.draw(win)
176     hairline2.draw(win)
177     hairline3.draw(win)
178     hairline4.draw(win)
179     hairline5.draw(win)
180     hairline6.draw(win)
181     hairline7.draw(win)
182     hairline8.draw(win)
183     hairline9.draw(win)
184     hairline10.draw(win)
185     hairline11.draw(win)
186     hairline12.draw(win)
187     hairline13.draw(win)
188     hairline14.draw(win)
189     hairline15.draw(win)
190     hairline16.draw(win)
191     hairline17.draw(win)
192     hairline18.draw(win)
193     hairline19.draw(win)
194     hairline20.draw(win)
195     hairline21.draw(win)
196     hairline22.draw(win)
197     hairline23.draw(win)
198     hairline24.draw(win)
199     hairline25.draw(win)
200     hairline26.draw(win)
201     hairline27.draw(win)
202     hairline28.draw(win)
203     win.getMouse()
204     
205 main()

《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

4、编写一个用圣诞树和雪人绘制冬季场景的程序。

解答

  1 # By: Megan Neuendorf
  2 
  3 from graphics import *
  4 
  5 def main():
  6     win=GraphWin("Winter Scene",500,500)
  7     win.setCoords(0,0,200,200)
  8     win.setBackground("blue")
  9 
 10     # Body of the snowman
 11     c1 = Circle(Point(50,40),40)
 12     c1.draw(win)
 13     c1.setFill("white")
 14     c1.setOutline("white")
 15 
 16     c2 = Circle(Point(50,100),30)
 17     c2.draw(win)
 18     c2.setFill("white")
 19     c2.setOutline("white")
 20 
 21     c3 = Circle(Point(50,145),20)
 22     c3.draw(win)
 23     c3.setFill("white")
 24     c3.setOutline("white")
 25 
 26     # Top Hat of Snowman
 27     r1 = Rectangle(Point(30,160),Point(70,165))
 28     r1.draw(win)
 29     r1.setFill("black")
 30 
 31     r2 = Rectangle(Point(40,165),Point(60,185))
 32     r2.draw(win)
 33     r2.setFill("black")
 34 
 35     # Eyes of the Snowman
 36     e1 = Circle(Point(42.5,150),2.5)
 37     e1.draw(win)
 38     e1.setFill("black")
 39 
 40     e2 = Circle(Point(57.5,150),2.5)
 41     e2.draw(win)
 42     e2.setFill("black")
 43 
 44     # Nose of the Snowman
 45     n = Polygon(Point(50,142.5),Point(50,137.5),Point(57.5,140))
 46     n.draw(win)
 47     n.setOutline("orange")
 48     n.setFill("orange")
 49 
 50     # Mouth of the Snowman
 51     m1 = Circle(Point(40,135),1)
 52     m1.draw(win)
 53     m1.setFill("black")
 54 
 55     m2 = Circle(Point(45,130),1)
 56     m2.draw(win)
 57     m2.setFill("black")
 58 
 59     m3 = Circle(Point(50,127.5),1)
 60     m3.draw(win)
 61     m3.setFill("black")
 62 
 63     m4 = Circle(Point(55,130),1)
 64     m4.draw(win)
 65     m4.setFill("black")
 66 
 67     m5 = Circle(Point(60,135),1)
 68     m5.draw(win)
 69     m5.setFill("black")
 70 
 71     # Buttons on the Snowman
 72     b1 = Circle(Point(50,115),3)
 73     b1.draw(win)
 74     b1.setFill("black")
 75 
 76     b2 = Circle(Point(50,105),3)
 77     b2.draw(win)
 78     b2.setFill("black")
 79 
 80     b3 = Circle(Point(50,95),3)
 81     b3.draw(win)
 82     b3.setFill("black")
 83 
 84     # Christmas Tree
 85     rect1 = Rectangle(Point(140,0),Point(160,25))
 86     rect1.draw(win)
 87     rect1.setOutline("brown")
 88     rect1.setFill("brown")
 89     
 90     t1 = Polygon(Point(100,25),Point(200,25),Point(150,65))
 91     t1.draw(win)
 92     t1.setOutline("forest green")
 93     t1.setFill("forest green")
 94 
 95     t2 = Polygon(Point(110,60),Point(190,60),Point(150,100))
 96     t2.draw(win)
 97     t2.setOutline("forest green")
 98     t2.setFill("forest green")
 99 
100     t3 = Polygon(Point(120,90),Point(180,90),Point(150,120))
101     t3.draw(win)
102     t3.setOutline("forest green")
103     t3.setFill("forest green")
104 
105     t4 = Polygon(Point(130,115),Point(170,115),Point(150,135))
106     t4.draw(win)
107     t4.setOutline("forest green")
108     t4.setFill("forest green")
109 
110     t5 = Polygon(Point(135,132.5),Point(165,132.5),Point(150,155))
111     t5.draw(win)
112     t5.setOutline("forest green")
113     t5.setFill("forest green")
114 
115     # Star on the Christmas Tree
116     n = Polygon(Point(150,152.5),Point(147.5,160),Point(140,162.5),Point(147.5,165),Point(150,172.5),Point(152.5,165),Point(160,162.5),Point(152.5,160))
117     n.draw(win)
118     n.setOutline("gold")
119     n.setFill("gold")
120 
121     # Circular Ornaments on Christmas Tree
122     o1 = Circle(Point(150,142.5),2.5)
123     o1.draw(win)
124     o1.setOutline("red")
125     o1.setFill("gold")
126 
127     o2 = Circle(Point(135,115),2.5)
128     o2.draw(win)
129     o2.setOutline("red")
130     o2.setFill("gold")
131 
132     o3 = Circle(Point(165,115),2.5)
133     o3.draw(win)
134     o3.setOutline("red")
135     o3.setFill("gold")
136 
137     o4 = Circle(Point(150,95),2.5)
138     o4.draw(win)
139     o4.setOutline("red")
140     o4.setFill("gold")
141 
142     o5 = Circle(Point(135,75),2.5)
143     o5.draw(win)
144     o5.setOutline("red")
145     o5.setFill("gold")
146 
147     o6 = Circle(Point(165,75),2.5)
148     o6.draw(win)
149     o6.setOutline("red")
150     o6.setFill("gold")
151 
152     o7 = Circle(Point(115,60),2.5)
153     o7.draw(win)
154     o7.setOutline("red")
155     o7.setFill("gold")
156 
157     o8 = Circle(Point(185,60),2.5)
158     o8.draw(win)
159     o8.setOutline("red")
160     o8.setFill("gold")
161 
162     o9 = Circle(Point(150,30),2.5)
163     o9.draw(win)
164     o9.setOutline("red")
165     o9.setFill("gold")
166 
167     # Diamond Ornaments on Christmas Tree
168     d1 = Polygon(Point(140,135),Point(142.5,132.5),Point(140,130),Point(137.5,132.5))
169     d1.draw(win)
170     d1.setOutline("gold")
171     d1.setFill("red")
172 
173     d2 = Polygon(Point(160,135),Point(162.5,132.5),Point(160,130),Point(157.5,132.5))
174     d2.draw(win)
175     d2.setOutline("gold")
176     d2.setFill("red")
177 
178     d3 = Polygon(Point(150,122.5),Point(152.5,120),Point(150,117.5),Point(147.5,120))
179     d3.draw(win)
180     d3.setOutline("gold")
181     d3.setFill("red")
182 
183     d4 = Polygon(Point(125,92.5),Point(127.5,90),Point(125,87.5),Point(122.5,90))
184     d4.draw(win)
185     d4.setOutline("gold")
186     d4.setFill("red")
187 
188     d5 = Polygon(Point(175,92.5),Point(177.5,90),Point(175,87.5),Point(172.5,90))
189     d5.draw(win)
190     d5.setOutline("gold")
191     d5.setFill("red")
192 
193     d6 = Polygon(Point(150,67.5),Point(152.5,65),Point(150,62.5),Point(147.5,65))
194     d6.draw(win)
195     d6.setOutline("gold")
196     d6.setFill("red")
197 
198     d7 = Polygon(Point(130,47.5),Point(132.5,45),Point(130,42.5),Point(127.5,45))
199     d7.draw(win)
200     d7.setOutline("gold")
201     d7.setFill("red")
202 
203     d8 = Polygon(Point(170,47.5),Point(172.5,45),Point(170,42.5),Point(167.5,45))
204     d8.draw(win)
205     d8.setOutline("gold")
206     d8.setFill("red")
207 
208     d9 = Polygon(Point(105,27.5),Point(107.5,25),Point(105,22.5),Point(102.5,25))
209     d9.draw(win)
210     d9.setOutline("gold")
211     d9.setFill("red")
212 
213     d10 = Polygon(Point(195,27.5),Point(197.5,25),Point(195,22.5),Point(192.5,25))
214     d10.draw(win)
215     d10.setOutline("gold")
216     d10.setFill("red")
217 
218     win.getMouse()
219     win.close()
220     
221 main()

《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

5、编写一个程序,在屏幕上绘制5 个骰子,是一把顺子(1,2,3,4,5 或2,3,4,5,6)。

解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin("Dice", width=800, height=400)
 5     win.setCoords(-10, -5, 10, 5)
 6 
 7     d1 = Rectangle(Point(-9, -1), Point(-7, 1))
 8     d1.draw(win)
 9     c = Circle(Point(-8, 0), 0.3)
10     c.setFill("red")
11     c.setOutline("white")
12     c.draw(win)
13     
14     d2 = d1.clone()
15     d2.move(4, 0)
16     d2.draw(win)
17     c = Circle(Point(-4.3, 0.3), 0.25)
18     c.setFill("blue")
19     c.setOutline("white")
20     c.draw(win)
21     c = c.clone()
22     c.move(0.6, -0.6)
23     c.draw(win)
24     
25     d3 = d2.clone()
26     d3.move(4, 0)
27     d3.draw(win)
28 
29     c = Circle(Point(-0.5, 0.5), 0.25)
30     c.setFill("blue")
31     c.setOutline("white")
32     c.draw(win)
33     c = c.clone()
34     c.move(0.5, -0.5)
35     c.draw(win)
36     c = c.clone()
37     c.move(0.5, -0.5)
38     c.draw(win)
39 
40     d4 = d3.clone()
41     d4.move(4, 0)
42     d4.draw(win)
43     c = Circle(Point(3.6, 0.4), 0.25)
44     c.setFill("red")
45     c.setOutline("white")
46     c.draw(win)
47     c = c.clone()
48     c.move(0.8, 0)
49     c.draw(win)
50     c = c.clone()
51     c.move(0, -0.8)
52     c.draw(win)
53     c = c.clone()
54     c.move(-0.8, 0)
55     c.draw(win)
56 
57     d5 = d4.clone()
58     d5.move(4, 0)
59     d5.draw(win)
60     c = Circle(Point(7.5, 0.5), 0.25)
61     c.setFill("blue")
62     c.setOutline("white")
63     c.draw(win)
64     c = c.clone()
65     c.move(1, 0)
66     c.draw(win)
67     c = c.clone()
68     c.move(0, -1)
69     c.draw(win)
70     c = c.clone()
71     c.move(-1, 0)
72     c.draw(win)
73     c = c.clone()
74     c.move(0.5, 0.5)
75     c.draw(win)
76 
77     win.getMouse()
78     win.close()
79 
80 main()

《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin("Dice", width=800, height=400)
 5     win.setCoords(-10, -5, 10, 5)
 6 
 7     d2 = Rectangle(Point(-9, -1), Point(-7, 1))
 8     d2.draw(win)
 9     c = Circle(Point(-8.3, 0.3), 0.25)
10     c.setFill("blue")
11     c.setOutline(color_rgb(240, 240, 240))
12     c.draw(win)
13     c = c.clone()
14     c.move(0.6, -0.6)
15     c.draw(win)
16     
17     d3 = d2.clone()
18     d3.move(4, 0)
19     d3.draw(win)
20     c = Circle(Point(-4.5, 0.5), 0.25)
21     c.setFill("blue")
22     c.setOutline(color_rgb(240, 240, 240))
23     c.draw(win)
24     c = c.clone()
25     c.move(0.5, -0.5)
26     c.draw(win)
27     c = c.clone()
28     c.move(0.5, -0.5)
29     c.draw(win)
30 
31     d4 = d3.clone()
32     d4.move(4, 0)
33     d4.draw(win)
34     c = Circle(Point(-0.4, 0.4), 0.25)
35     c.setFill("red")
36     c.setOutline(color_rgb(240, 240, 240))
37     c.draw(win)
38     c = c.clone()
39     c.move(0.8, 0)
40     c.draw(win)
41     c = c.clone()
42     c.move(0, -0.8)
43     c.draw(win)
44     c = c.clone()
45     c.move(-0.8, 0)
46     c.draw(win)
47 
48     d5 = d4.clone()
49     d5.move(4, 0)
50     d5.draw(win)
51     c = Circle(Point(3.5, 0.5), 0.25)
52     c.setFill("blue")
53     c.setOutline(color_rgb(240, 240, 240))
54     c.draw(win)
55     c = c.clone()
56     c.move(1, 0)
57     c.draw(win)
58     c = c.clone()
59     c.move(0, -1)
60     c.draw(win)
61     c = c.clone()
62     c.move(-1, 0)
63     c.draw(win)
64     c = c.clone()
65     c.move(0.5, 0.5)
66     c.draw(win)
67 
68     d6 = d5.clone()
69     d6.move(4, 0)
70     d6.draw(win)
71     c = Circle(Point(7.6, 0.6), 0.25)
72     c.setFill("blue")
73     c.setOutline(color_rgb(240, 240, 240))
74     c.draw(win)
75     c = c.clone()
76     c.move(0.8, 0)
77     c.draw(win)
78     c = c.clone()
79     c.move(0, -0.6)
80     c.draw(win)
81     c = c.clone()
82     c.move(-0.8, 0)
83     c.draw(win)
84     c = c.clone()
85     c.move(0, -0.6)
86     c.draw(win)
87     c = c.clone()
88     c.move(0.8, 0)
89     c.draw(win)
90 
91     win.getMouse()
92     win.close()
93 
94 main()

《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

6、修改图形终值程序,让输入(本金和APR)也用Entry 对象以图形方式完成。

解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin("Investment Growth Chart", 640, 480)
 5     win.setBackground("white")
 6     # Set coordinates for easy display of prompts.
 7     win.setCoords(0,0,10,10)
 8 
 9     # Display prompts
10     t1 = Text(Point(5,8), "Plotting a 10 year investment")
11     t1.setSize(14)
12     t1.draw(win)
13 
14     t2 = Text(Point(5,7.5), "Enter the information below and then click anywhere")
15     t2.setSize(14)
16     t2.draw(win)
17 
18     t3 = Text(Point(2,6), "Initial Principal:")
19     t3.setSize(14)
20     t3.draw(win)
21 
22     prinBox = Entry(Point(4.5,6), 6)
23     prinBox.draw(win)
24     prinBox.setText("2000")
25 
26     t4 = Text(Point(2,4), "Annual Interest Rate:")
27     t4.setSize(14)
28     t4.draw(win)
29 
30     aprBox = Entry(Point(4.5,4), 6)
31     aprBox.setText("0.05")
32     aprBox.draw(win)
33 
34     # wait for mouse click and get values
35     win.getMouse()
36     principal = float(prinBox.getText())
37     apr = float(aprBox.getText())
38 
39     # Erase the prompts
40     t1.undraw()
41     t2.undraw()
42     t3.undraw()
43     t4.undraw()
44     prinBox.undraw()
45     aprBox.undraw()
46 
47 
48     # Set Window coords for drawing the graph.
49     win.setCoords(-1.75,-200, 11.5, 10400)
50 
51     # Create a graphics window with labels on left edge
52     Text(Point(-1, 0), ' 0.0K').draw(win)
53     Text(Point(-1, 2500), ' 2.5K').draw(win)
54     Text(Point(-1, 5000), ' 5.0K').draw(win)
55     Text(Point(-1, 7500), ' 7.5k').draw(win)
56     Text(Point(-1, 10000), '10.0K').draw(win)
57 
58     # Draw bar for initial principal
59     bar = Rectangle(Point(0, 0), Point(1, principal))
60     bar.setFill("green")
61     bar.setWidth(2)
62     bar.draw(win)
63     
64     # Draw a bar for each subsequent year
65     for year in range(1, 11):
66         principal = principal * (1 + apr)
67         bar = Rectangle(Point(year, 0), Point(year+1, principal))
68         bar.setFill("green")
69         bar.setWidth(2)
70         bar.draw(win)
71 
72     win.getMouse()
73     win.close()
74 
75 main()

7、圆的交点。
编写一个计算圆与水平线的交点的程序,并以文本和图形方式显示信息。
输入:圆的半径和线的y 截距。
输出:在坐标为从(−10,−10)到(10,10)的窗口中,以(0, 0)为中心,以给定半径绘制
的圆。
用给定的y 轴截取一根水平线穿过窗口。
以红色绘制两个交点。
打印出交叉点的x 值。
公式:
《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

解答

 1 from graphics import *
 2 import math
 3 
 4 def main():
 5     radius = float(input("Please enter the radius of the circle: "))
 6     yinter = float(input("Please enter the y-intercept of the line: "))
 7 
 8     win = GraphWin("Circle Intersection")
 9     win.setCoords(-10, -10, 10, 10)
10 
11     Circle(Point(0, 0), radius).draw(win)
12     Line(Point(-10, yinter), Point(10, yinter)).draw(win)
13 
14     x = math.sqrt(radius * radius - yinter * yinter)
15     print("X values of intersection", -x, x)
16 
17     p1 = Circle(Point(x, yinter), 0.25)
18     p1.setOutline("red")
19     p1.setFill("red")
20     p1.draw(win)
21 
22     p2 = p1.clone()
23     p2.move(-2 * x, 0)
24     p2.draw(win)
25 
26     win.getMouse()
27     win.close()
28 
29 main()
30 
31 # Output:
32 # Please enter the radius of the circle: 5
33 # Please enter the y-intercept of the line: 3
34 # X values of intersection -4.0 4.0

《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

8、线段信息。
该程序允许用户绘制线段,然后显示关于线段的一些图形和文本信息。
输入:两次鼠标点击线段的终点。
输出:以青色绘制线段的中点。
绘制线段。
打印线的长度和斜率。
公式:
《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

解答

 1 from graphics import *
 2 import math
 3 
 4 def main():
 5     win = GraphWin("Triangle", 400, 400)
 6     win.setCoords(-5, -5, 5, 5)
 7 
 8     draw_coord(win)    # 这个函数定义见文章末尾
 9 
10     pt1 = win.getMouse()
11     pt1.draw(win)
12     pt2 = win.getMouse()
13     pt2.draw(win)
14     line = Line(pt1, pt2)
15     line.draw(win)
16     mark = Circle(line.getCenter(),0.075)
17     mark.setFill("cyan")
18     mark.draw(win)
19 
20     dx = pt2.getX() - pt1.getX()
21     dy = pt2.getY() - pt1.getY()
22     slope = dy / dx
23     length = math.sqrt(dx * dx + dy * dy)
24     
25     message = Text(Point(0, -4), "Point1:({}, {})\nPoint2:({}, {})\n长度:{}\n斜率:{}".format(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY(), length, slope))
26     message.setSize(8)
27     message.setTextColor(color_rgb(144, 144, 144))
28     message.draw(win)
29 
30     win.getMouse()
31     win.close()
32 
33 main()

《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

9、矩形信息。

此程序显示有关用户绘制的矩形的信息。
输入:两次鼠标点击作为矩形的对角。
输出:绘制矩形。
打印矩形的周长和面积。
公式:面积=(长度)(宽度)
周长= 2(长度+宽度)

解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin("Rectangle", 400, 400)
 5     win.setCoords(-5, -5, 5, 5)
 6 
 7     draw_coord(win)
 8 
 9     pt1 = win.getMouse()
10     pt2 = win.getMouse()
11     rect = Rectangle(pt1, pt2)
12     rect.draw(win)
13 
14     height = abs(pt2.getY() - pt1.getY())
15     length = abs(pt2.getX() - pt1.getX())
16 
17     area = height * length
18     perimeter = 2 * (height + length)
19 
20     message = Text(Point(0, -4), "Point1:({}, {})\nPoint2:({}, {})\n面积:{}\n周长:{}".format(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY(), area, perimeter))
21     message.setSize(8)
22     message.setTextColor(color_rgb(144, 144, 144))
23     message.draw(win)
24 
25     win.getMouse()
26     win.close()
27 
28 main()

《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

10、三角形信息。
与上一个问题相同,但三角形的顶点有三次点击。
公式:关于周长,可参阅线段问题中的长度。

《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

解答

 1 from graphics import *
 2 import math
 3 
 4 def main():
 5     win = GraphWin("Triangle", 400, 400)
 6     win.setCoords(-5, -5, 5, 5)
 7 
 8     draw_coord(win)
 9 
10     pt1 = win.getMouse()
11     pt1.draw(win)
12     pt2 = win.getMouse()
13     pt1.undraw()
14     line = Line(pt1, pt2)
15     line.draw(win)
16     pt3 = win.getMouse()
17     line.undraw()
18     tri = Polygon(pt1, pt2, pt3)
19     tri.draw(win)
20     
21     dx1 = pt1.getX() - pt2.getX()
22     dy1 = pt1.getY() - pt2.getY()
23     dx2 = pt2.getX() - pt3.getX()
24     dy2 = pt2.getY() - pt3.getY()
25     dx3 = pt3.getX() - pt1.getX()
26     dy3 = pt3.getY() - pt1.getY()
27     
28     a = math.sqrt(dx1 * dx1 + dy1 * dy1)
29     b = math.sqrt(dx2 * dx2 + dy2 * dy2)
30     c = math.sqrt(dx3 * dx3 + dy3 * dy3)
31     
32     perimeter = a + b + c
33     s = perimeter / 2
34     area = math.sqrt(s * (s - a) * (s - b) * (s - c))
35 
36     message = Text(Point(0, -4), "Point1:({}, {})\nPoint2:({}, {})\nPoint3:({}, {})\n面积:{}\n周长:{}".format(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY(), pt3.getX(), pt3.getY(), area, perimeter))
37     message.setSize(8)
38     message.setTextColor(color_rgb(144, 144, 144))
39     message.draw(win)
40 
41     win.getMouse()
42     win.close()
43 
44 main()

《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

11、五次点击的房子。

​ 编写一个程序,允许用户通过五次鼠标点击,绘制一个简单的房子。前两次点击是房子的矩形框架的对角。第三次点击指出矩形门的顶部边缘的中心。门的宽度应为房屋框架宽度的1/5。门的边框应从顶部的转角延伸到框架的底部。第四次点击指出正方形窗口的中心。窗口的宽度是门的一半。最后一次点击指出屋顶的顶点。屋顶的边缘将从顶点延伸到房屋框架的顶部边缘的转角。
解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin(width=500, height=500)
 5     
 6     frameLL = win.getMouse()
 7     frameLL.draw(win)
 8     frameUR = win.getMouse()
 9     frameLL.undraw()
10     Rectangle(frameLL, frameUR).draw(win)
11 
12     doorUC = win.getMouse()
13     houseWidth = frameUR.getX() - frameLL.getX()
14     doorWidth = houseWidth * 0.2
15     doorLL = Point(doorUC.getX() - doorWidth / 2, frameLL.getY())
16     doorUR = Point(doorUC.getX() + doorWidth / 2, doorUC.getY())
17     Rectangle(doorLL, doorUR).draw(win)
18 
19     windowCenter = win.getMouse()
20     windowWidth = doorWidth / 2
21     windowLL = Point(windowCenter.getX() - windowWidth / 2, windowCenter.getY() - windowWidth / 2)
22     windowUR = Point(windowCenter.getX() + windowWidth / 2, windowCenter.getY() + windowWidth / 2)
23     Rectangle(windowLL, windowUR).draw(win)
24 
25     peak = win.getMouse()
26     roofL = Line(Point(frameLL.getX(), frameUR.getY()), peak)
27     roofR = Line(peak, frameUR)
28     roofL.draw(win)
29     roofR.draw(win)
30 
31     win.getMouse()
32     win.close()
33 
34 main()

《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案

draw_coord() 实现:
 1 def draw_coord(win):
 2    for i in range(5, -6, -1):
 3        l = Line(Point(-5, i), Point(5, i))
 4        l.setFill(color_rgb(192, 192, 192))
 5        l.draw(win)
 6    for i in range(-5, 6, 1):
 7        l = Line(Point(i, 5), Point(i, -5))
 8        l.setFill(color_rgb(192, 192, 192))
 9        l.draw(win)
10 
11    x_axis = Line(Point(-5, 0), Point(5, 0))
12    x_axis.setArrow("last")
13    x_axis.setFill(color_rgb(144, 144, 144))
14    x_axis.draw(win)
15    x_text = Text(Point(4.75, -0.25), "x")
16    x_text.setFill(color_rgb(144, 144, 144))
17    x_text.draw(win)
18 
19    y_axis = Line(Point(0, -5), Point(0, 5))
20    y_axis.setArrow("last")
21    y_axis.setFill(color_rgb(144, 144, 144))
22    y_axis.draw(win)
23    y_text = Text(Point(0.25, 4.75), "y")
24    y_text.setFill(color_rgb(144, 144, 144))
25    y_text.draw(win)
26    
27    base_point = Text(Point(-0.25, -0.25), "0")
28    base_point.setFill(color_rgb(144, 144, 144))
29    base_point.draw(win)