python學習——練習題(4)
阿新 • • 發佈:2017-12-07
某年 時間 ftime true 題目 error == highlight exc
""" 題目:輸入某年某月某日,判斷這一天是這一年的第幾天? """ import datetime import time from functools import reduce def calculate1(t): """ 直接利用python的datetime模塊計算 :param t: :return: """ print("計算一", end=":") print(t.strftime("%j")) def calculate2(t): """ 自己手動計算一下 :param t: :return: """ print("計算二", end=":") days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] daysLeap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] year = t.year if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0): print(sum(daysLeap[:t.month - 1]) + t.day) else: print(sum(days[:t.month - 1]) + t.day) def calculate3(t): """ 高手簡化後的calculate2 :param t: :return: """ print("計算三", end=":") days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] year = t.year if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0): days[1] = 29 print(sum(days[0: t.month - 1]) + t.day) def calculate4(t): """ 利用字典來計算 :param t: :return: """ print("計算四", end=":") dayDict = {0: 0, 1: 31, 2: 59, 3: 90, 4: 120, 5: 151, 6: 181, 7: 212, 8: 243, 9: 273, 10: 304, 11: 334, 12: 365} year = t.year d = dayDict[t.month - 1] + t.day if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0): d += 1 print(d) def calculate5(t): """ 利用time模塊來計算,註意和datetime模塊進行區分 :param t: :return: """ print("計算五", end=":") t = time.strptime(t.strftime("%Y-%m-%d"), "%Y-%m-%d") print(t[7]) def calculate6(t): """ 利用datetime的時間相減來計算 :param t: :return: """ print("計算六", end=":") t1 = datetime.date(t.year, 1, 1) t2 = t - t1 print(t2.days + 1) def calculate7(t): """ 利用time的時間相減來計算,註意與datetime進行區分,它不能直接減,需要轉成時間戳才能減 以為時間戳是以1970年為基點計算的,所以該方法只能計算1970以後(不包括1970)的時間 :param t: :return: """ print("計算七", end=":") t1 = time.strptime(t.strftime("%Y-01-01"), "%Y-%m-%d") t1 = time.mktime(t1) t = time.strptime(t.strftime("%Y-%m-%d"), "%Y-%m-%d") t = time.mktime(t) t2 = t - t1 t2 = t2 // (3600 * 24) print(int(t2) + 1) def calculate8(t): """ 利用reduce函數來計算,中間有用到三元運算符 在Python 3裏,reduce()函數已經被從全局名字空間裏移除了,它現在被放置在fucntools模塊裏 用的話要 先引入 from functools import reduce :param t: :return: """ print("計算八", end=":") year = t.year days = [0, 31, 28 if year % 4 else 29 if year % 100 else 28 if year % 400 else 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] print(reduce(lambda a, b: a + b, days[0: t.month]) + t.day) def calculate9(t): """ 利用位運算來計算閏年: 分析 year&3 等價於 year%4:因為二進制轉十進制是:2**0+2**1+2**2+。。。,可見2**2之後的都可以被4整除 同理 year&15 等價 year%16 根據閏年計算規則我們可以知道:不能被4整除的年份肯定不是閏年,而能被4整除又能被25整數但不能再被16整數的也不是閏年,其余全是閏年 可得 year%4 or year%16 and !year%25 這些都不是閏年,反之!(year%4 or year%16 and !year%25)為閏年 轉為位運算!(year&3 or year&15 and !(year%25)) :param t: :return: """ print("計算九", end=":") days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] year = t.year if not(year & 3 or year & 15 and not(year % 25)): days[1] = 29 print(sum(days[0: t.month - 1]) + t.day) def answer(): """ 通過try來判斷輸入的日期是否正確 :return: """ year = input("輸入年:") if year == "q": return month = input("輸入月:") day = input("輸入日:") try: t = datetime.date(int(year), int(month), int(day)) calculate1(t) calculate2(t) calculate3(t) calculate4(t) calculate5(t) calculate6(t) calculate7(t) calculate8(t) calculate9(t) except ValueError: print("輸入的日期錯誤") print("繼續,或輸入q推出") answer() answer()
python學習——練習題(4)