辅导python、辅导python计算日期

- 首页 >> Python编程

实验:2.计算日期实验内容:计算给定日期的星期实验目的:掌握python中if语句的使用          掌握字符串的切片的方法          掌握时间合理性的判断In this program you will prompt the user to enter the day, month, and year. Your program will print out the day of the week for that date. Here is a sample output for the program: (此程序中,输入日期,时间和年份,程序输出此日期的星期,其格式如下:)Enter year: 1960Enter month: 12Enter day: 12The day is Monday.Assume that all the input values will be positive integers. The day will be in the range 1 through 31. The month will be in the range 1 through 12 where 1 is for January and 12 is for December. The year will be a four digit number in the range 1900 through 2100. (假设输入的值都为正整数,日期在1到31之间,月份在1到12之间,1代表1月,12代表12月,输入的年份为四位数字在1900到2100之间。)Your program will check the following: (程序检测如下条件:)•The year is between 1900 and 2100. (年份在1900到2100)•The month is in the range 1 and 12. (月份在1到12)•The day is in the range appropriate for that month. For months other than February, the range of days will be from 1 to 30 or 31 days. The range of days for February is between 1 and 28 or 29 depending on whether the year is a leap year or not. (日期是这个月份的合理日期,除了二月份,值在1到30或者31,二月份的值由是否闰年决定为28或者29)程序先判断输入的各个值是否合理,如果不合理,提示错误并退出程序,退出程序用exit()函数This algorithm was developed by Rev. Zeller. Let us define the quantities a, b, c, and d as follows:(程序的算法如下:) •a ≡ the month of the year, with March = 1, April = 2, … with January = 11, and February = 12 of the preceding year •b ≡ the day of the month(日期)•c ≡ the year of the century(年)•d ≡ the century, though not in the conventional sense. For example, after making adjustments for the months of January and February, for all years like 1910, 1949, 1984, the value of d = 19. And for years like 2003, 2010, 2025, the value of d = 20. (年份的前两个值)Important: In our calendar, the year begins in January and ends in December. In the calendar, used in the algorithm, the year begins in March and ends in February. Your program should internally make the adjustment and not expect the user to know this. For example, if in our calendar we have January 2009 (month = 1 and year = 2009), the program will make the adjustment so that month = 11 and year = 2008. If you do not make the adjustment you will not get the right result. (提示:在日历中,开始为1月,结束为12月,而程序中,开始为三月,结束为二月,所以程序要对用户输入的月份和年份进行调整,而不期望用户知道这些知识。如2009.1调整完为2008.11)例如:For example, 31 July 1929, gives a = 5, b = 31, c = 29, and d = 19. Similarly, 3 January 1988, gives a = 11, b = 3, c = 87, and d = 19下面为具体算法:. Now compute the following quantities: •w = (13 * a - 1 ) // 5 •x = c // 4 •y = d // 4 •z = w + x + y + b + c - 2 * d •r = z % 7 •r = (r + 7) % 7 [to take care of negative values of r]r gives the day of the week. r = 0 represents Sunday, r = 1 represents Monday, and so on. The output from your file should match the following statement: (r给出具体的星期,如果是0代表星期日,1代表星期1,以此类推)The day is Monday.Where Monday may be replaced by any day of the week.

站长地图