# ============================================================================== # 通过 D:\文档\电子文档\个人\脚本库\PythonScript\第7章.py + 题号 运行 # 程序名: # 第7章习题 # 目的: # # 修订记录: # 日期 编程者 改动描述 # =================== ============= ===================================== # 2021-07-27 17:51:57 Sola 7-1 ~ 7-10 略 # 2021-07-27 19:36:11 Sola 7-11 闰年计算 # 2021-07-27 19:44:24 Sola 7-12 日期有效性判断 # 2021-07-27 19:58:14 Sola 7-13 计算某一天是一年中的第几天 # 2021-07-27 20:11:04 Sola 7-14 ~ 7-15 略,做过了 # 2021-07-27 20:18:46 Sola 7-16 箭靶计分 # 2021-07-27 20:39:32 Sola 7-17 弹球 # 2021-07-27 21:54:58 Sola 7-18 略 def IsLeapYear(year): if year != int(year): print("错误:请输入整数年份!") return -1 if year % 4 == 0 and year % 400 != 0: return True else: return False def IsTrueDate(date): # need function: IsLeapYear dateList = date.split('/') day = int(dateList[0]) month = int(dateList[1]) year = int(dateList[2]) if month > 12 or month < 0: # print("错误:月份范围错误,请检查输入!") return False if IsLeapYear(year): monthList = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] else: monthList = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if day > monthList[month - 1] or day < 0: # print("错误:日期范围错误,请检查输入!") return False return True def GetDateNum(date): # need function: IsTrueDate, IsLeapYear if IsTrueDate(date): dateList = date.split('/') day = int(dateList[0]) month = int(dateList[1]) year = int(dateList[2]) if IsLeapYear(year): monthList = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] else: monthList = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] dateNum = 0 for i in range(month - 1): dateNum = dateNum + monthList[i] return dateNum + day else: return -1 def Test7_11(): year = int(input("请输入年份:")) if IsLeapYear(year): print("{0}是闰年".format(year)) else: print("{0}不是闰年".format(year)) def Test7_12(): date = input("请按照 dd/mm/yyyy 格式输入日期:") if IsTrueDate(date): print("{0} 是有效的日期!".format(date)) else: print("{0} 是无效的日期!".format(date)) def Test7_13(): date = input("请按照 dd/mm/yyyy 格式输入日期:") dateNum = GetDateNum(date) if dateNum > 0: print("{0} 是该年份的第{1}天".format(date, dateNum)) else: print("错误:输入的日期非法!") def Test7_16(): from graphics import GraphWin, Circle, Text, Point win = GraphWin() def draw_circle(r, s_fill): shape = Circle(Point(100, 90), r) shape.setFill(s_fill) shape.setOutline("black") shape.clone().draw(win) draw_circle(75, "white") draw_circle(60, "black") draw_circle(45, "blue") draw_circle(30, "red") draw_circle(15, "yellow") scoreSum = 0 scoreText = Text(Point(15, 15), '') scoreText.draw(win) for i in range(5): target = win.getMouse() score = max(9 - ((target.getX() - 100) ** 2 + (target.getY() - 90) ** 2) ** 0.5 // 15 * 2, 0) scoreText.setText(str(int(score))) scoreSum = scoreSum + score scoreText.undraw() Text(Point(100, 180), "Your average score is {0}!\n".format(scoreSum / 5) + "Click again to quit").draw(win) win.getMouse() win.close() def Test7_17(): from graphics import GraphWin, Circle, Text, Point, update h0 = 144 x0 = 20 xx = 0 win = GraphWin() win.setCoords(0, 0, 200, 200) ball = Circle(Point(x0, h0), 20) ball.setFill("blue") ball.setOutline("black") ball.draw(win) n = 5000 for i in range(n): ht = (144 - (i % 24 - 12) ** 2) * (n - i) / n + 20 vx = 7 * (n - i) / n xx = xx + vx xt = 20 + abs(xx % 320 - 160) dh = ht - h0 dx = xt - x0 h0 = ht x0 = xt ball.move(dx, dh) update(30) Text(Point(100, 100), "Click to quit").draw(win) win.getMouse() import sys def Run(num): eval("Test7_" + num + "()") Run(sys.argv[1])