Última actividad 10 months ago

此程式根據輸入的出生日期,判定並返回對應的星座名稱。它使用 Python 的 datetime 來解析日期,並比對星座日期範圍。可應用於星座分析、個性測試或相關娛樂應用。

timmy revisó este gist 10 months ago. Ir a la revisión

1 file changed, 132 insertions

constellation.py(archivo creado)

@@ -0,0 +1,132 @@
1 + #!/usr/bin/env python
2 + # -*- coding: utf-8 -*-
3 +
4 + """
5 + constellation.py: Description of what this script does.
6 +
7 + Author: Timmy
8 + Copyright: Copyright 2022, Timmy
9 + License: MIT
10 + Version: 1.0
11 + """
12 +
13 +
14 + from datetime import datetime
15 +
16 + # 嘗試載入 icecream 套件中的 ic 函數
17 + try:
18 + from icecream import ic
19 +
20 + # 配置 ic 函數的輸出,包括上下文訊息
21 + ic.configureOutput(includeContext=True)
22 +
23 + # 如果 icecream 套件沒有安裝,則會產生 ImportError
24 + except ImportError:
25 + # 如果沒有安裝 icecream 套件,則定義一個名為 ic 的 lambda 函數
26 + # 這個函數的作用是在沒有載入 icecream 套件時,提供一個替代的方式來輸出訊息和變數
27 + ic = lambda *a: None if not a else (a[0] if len(a) == 1 else a) # noqa
28 +
29 +
30 + class Constellation:
31 + def __init__(self):
32 + """
33 + Aries: March 21-April 19
34 + Taurus: April 20-May 20
35 + Gemini: May 21-June 20
36 + Cancer: June 21-July 22
37 + Leo: July 23-August 22
38 + Virgo: August 23-September 22
39 + Libra: September 23-October 22
40 + Scorpio: October 23-November 21
41 + Sagittarius: November 22-December 21
42 + Capricorn: December 22-January 19
43 + Aquarius: January 20-February 18
44 + Pisces: February 19-March 20
45 + """
46 +
47 + # 定義每個星座的日期範圍
48 + self.constellations = [
49 + ("摩羯座", "12-22", "01-19"),
50 + ("水瓶座", "01-20", "02-18"),
51 + ("雙魚座", "02-19", "03-20"),
52 + ("牡羊座", "03-21", "04-19"),
53 + ("金牛座", "04-20", "05-20"),
54 + ("雙子座", "05-21", "06-20"),
55 + ("巨蟹座", "06-21", "07-22"),
56 + ("獅子座", "07-23", "08-22"),
57 + ("處女座", "08-23", "09-22"),
58 + ("天秤座", "09-23", "10-22"),
59 + ("天蠍座", "10-23", "11-21"),
60 + ("射手座", "11-22", "12-21"),
61 + ]
62 +
63 + def get_month_and_day(self, date):
64 + """
65 + 這個函式會接收一個日期,並回傳這個日期的月份和日期。
66 +
67 + Args:
68 + date (str): 一個日期,格式為 YYYY-MM-DD。
69 +
70 + Returns:
71 + tuple: 一個元組,包含月份和日期。
72 + """
73 +
74 + # 把日期轉換為日期物件
75 + date_obj = datetime.strptime(date, "%Y-%m-%d")
76 +
77 + # 取得月份和日期
78 + month = date_obj.month
79 + day = date_obj.day
80 +
81 + # 回傳月份和日期
82 + return (month, day)
83 +
84 + def get_constellation(self, date):
85 + """
86 + 這個函數的作用是根據輸入的日期,返回該日期所屬的星座。
87 +
88 + Args:
89 + date (str): 一個日期,格式為 YYYY-MM-DD。
90 +
91 + Returns:
92 + str: 返回星座的名稱
93 + """
94 +
95 + # 取得月份和日期
96 + month, day = self.get_month_and_day(date)
97 +
98 + # 把傳入的月份和日期轉換為日期物件
99 + birth_date = datetime.strptime(f"{month}-{day}", "%m-%d")
100 +
101 + # 找出生日所屬的星座
102 + for constellation in self.constellations:
103 + name, start, end = constellation
104 + start_date = datetime.strptime(start, "%m-%d")
105 + end_date = datetime.strptime(end, "%m-%d")
106 + # if birth_date >= start_date and birth_date <= end_date:
107 + # return name
108 + if start_date <= birth_date <= end_date:
109 + return name
110 +
111 + # 如果沒找到,則回傳空字串
112 + return ""
113 +
114 +
115 + # 檢查當前程式是否被作為主程式執行
116 + if __name__ == "__main__":
117 +
118 + # 建立 Constellation 類別的物件
119 + constellation = Constellation()
120 +
121 + # 使用 get_constellation 方法查詢 5 月 25 日出生的人所屬的星座
122 + result = constellation.get_constellation("1995-05-25")
123 + print(result) # 輸出 '雙子座'
124 +
125 + result = constellation.get_constellation("1977-10-12")
126 + print(result) # 輸出 '天秤座'
127 +
128 + result = constellation.get_constellation("1981-08-29")
129 + print(result) # 輸出 '處女座'
130 +
131 + result = constellation.get_constellation("2016-02-16")
132 + print(result) # 輸出 '水瓶座'
Siguiente Anterior