# ============================================================================== # 通过 D:\文档\电子文档\个人\脚本库\PythonScript\第6章.py + 题号 运行 # 程序名: # 第6章习题 # 目的: # # 修订记录: # 日期 编程者 改动描述 # =================== ============= ===================================== # 2021-07-26 13:52:58 Sola 6-1 打印歌曲“Old MacDonald”的歌词 # 2021-07-26 16:21:07 Sola 6-2 略 # 2021-07-26 16:21:16 Sola 6-3 给定球体半径,求表面积和体积 # 2021-07-26 16:28:27 Sola 6-4 计算前N个自然数的和、立方和 # 2021-07-26 16:35:53 Sola 6-5 披萨饼单价 # 2021-07-26 18:32:19 Sola 6-6 三角形面积 # 2021-07-26 18:32:42 Sola 6-7 斐波纳契数 # 2021-07-26 18:35:29 Sola 6-8 平方根估计 # 2021-07-26 18:42:07 Sola 6-9 返回分数字母等级 # 2021-07-26 18:47:48 Sola 6-10 返回首字母缩略词 # 2021-07-26 18:55:52 Sola 6-11 返回列表数字平方 # 2021-07-26 21:42:06 Sola 6-12 返回数字列表的和 # 2021-07-26 18:56:18 Sola 6-13 将字符串列表转化为数字 # 2021-07-26 21:44:27 Sola 6-14 从文件读取数字计算平方和 # 2021-07-27 00:52:48 Sola 6-15 显示简单笑脸 # 2021-07-27 00:53:05 Sola 6-16 给图片点选加笑脸 # 2021-07-27 00:53:21 Sola 6-17 移动图形到点选位置 from graphics import GraphWin def test6_1(): def print_Old_MacDonald(animal, barking): print("Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!\n" + "And on that farm he had a {0}, Ee-igh, Ee-igh, Oh!\n".format(animal) + "With a {0}, {0} here and a {0}, {0} there.\n".format(barking) + "Here a {0}, there a {0}, everywhere a {0}, {0}.\n".format(barking) + "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!\n") print_Old_MacDonald("bee", "buzz") print_Old_MacDonald("bird", "tweet") print_Old_MacDonald("cat", "meow") print_Old_MacDonald("hen", "cluck") print_Old_MacDonald("cuckoo", "cuckoo") def test6_3(): from math import pi def sphereArea(radius): return 4 * pi * radius ** 2 def sphereVolume(radius): return 4 / 3 * pi * radius ** 3 radius=1 print("半径为{0}的球体,其表面积为{1:5.3f},体积为{2:5.3f}" .format(radius, sphereArea(radius), sphereVolume(radius))) def test6_4(): def sumN(n): result = 0 for i in range(n + 1): result = result + i return result def sumNCubes(n): result = 0 for i in range(n + 1): result = result + i ** 3 return result n = 10 print("前{0}位自然数的和为{1},立方和为{2}" .format(n, sumN(n), sumNCubes(n))) def test6_5(): def pizzaUnitPrise(r, prise): from math import pi return prise / (4 * pi * r ** 2) def test6_6(): def triangleArea(a, b, c): from math import sqrt s = (a + b + c) / 2 return sqrt(s * (s - a) * (s - b) * (s - c)) def test6_7(): def Fibonacci(n): temp = 0 result = 1 for i in range(n - 1): temp, result = result, temp + result return result print("斐波那契数列第", 10, "项为:", Fibonacci(10)) def test6_8(): def nextGuess(guess,x): result = guess / 2 for i in range(x): result = (result + guess / result) / 2 return result print("{0}的平方根迭代{1}次的估计值为{2:5.3f}".format(10, 10, nextGuess(10, 10))) def test6_9(): def grade(score): grade = ["F", "F", "F", "F", "F", "F", "D", "C", "B", "A", "A"] return grade[int(score / 10)] score = float(input("请输入你的分数:")) print("你的分数对应的等级为:{0}".format(grade(score))) def test6_10(): def acronym(phrase): shortName = "" for i in phrase.split(): shortName = shortName + i[0].upper() return shortName inputStr = input("请输入一个短语:") print("'{0}' 的缩写为:{1}".format(inputStr, acronym(inputStr))) def test6_11(): def squareEach(nums): for num in range(len(nums)): nums[num] = nums[num] ** 2 nums = list(range(10)) squareEach(nums) print(nums) def test6_12(): def sumList(nums): result = 0 for num in range(len(nums)): result = result + nums[num] return result nums = list(range(10)) print("列表数字的和为:{0}".format(sumList(nums))) def test6_13(): def toNumbers(strList): for i in range(len(strList)): strList[i] = float(strList[i]) convertList = ["2021", "2020", "2019", "2018"] print("这是转换之前的列表:{0}".format(convertList)) toNumbers(convertList) print("这是转换之后的列表:{0}".format(convertList)) def test6_14(): from tkinter.filedialog import askopenfilename def sumSquare(nums): result = 0 for num in range(len(nums)): result = result + float(nums[num]) ** 2 return result print("请选择需要打开的文件:") nums = open(askopenfilename(), 'r').readlines() print("打开文件中数字的平方和为:{0}".format(sumSquare(nums))) def test6_15(): from graphics import GraphWin, Text, Point def drawFace(center, size, win): face = Text(center, "笑脸") face.setSize(size) face.clone().draw(win) win = GraphWin("显示笑脸") drawFace(Point(10, 10), 10, win) drawFace(Point(100, 100), 36, win) drawFace(Point(160, 30), 30, win) win.getMouse() def test6_16(): from graphics import GraphWin, Point, Circle, Line, Image from tkinter.filedialog import askopenfilename def drawFace(center, size, win): Face = Circle(center, size) Face.setFill('yellow') Face.clone().draw(win) Line(Point(center.getX() + size * 0.2, center.getY() + size * 0.3), Point(center.getX() + size * 0.5, center.getY() + size * 0.3)).draw(win) Line(Point(center.getX() - size * 0.2, center.getY() + size * 0.3), Point(center.getX() - size * 0.5, center.getY() + size * 0.3)).draw(win) def clickDrawFace(win): center = win.getMouse() side = win.getMouse() size = ((center.getX() - side.getX()) ** 2 + (center.getY() - side.getY()) ** 2) ** 0.5 drawFace(center, size, win) print("请选择需要遮挡的图片,注意只支持GIF格式!") imageDir = askopenfilename() imageFile = Image(Point(0, 0), imageDir) imageHeight, imageWidth = imageFile.getHeight(), imageFile.getWidth() win = GraphWin("没脸见人系列", width=imageWidth, height=imageHeight) win.setCoords(-imageWidth / 2, -imageHeight / 2, imageWidth / 2, imageHeight / 2) imageFile.draw(win) times = int(input("请问需要遮挡的次数:")) for i in range(times): clickDrawFace(win) win.getMouse() def test6_17(): from graphics import GraphWin, Point, Circle def moveTo(shape, newCenter): oldCenter = shape.getCenter() shape.move(newCenter.getX() - oldCenter.getX(), newCenter.getY() - oldCenter.getY()) win = GraphWin("移动图形", width=800, height=600) draw = Circle(Point(100, 100), 100) draw.draw(win) for i in range(10): moveTo(draw, win.getMouse()) win.getMouse() import sys def run(num): eval("test6_" + num + "()") run(sys.argv[1])