用python查找星座
时间: 2023-05-26 19:06:13 浏览: 38
以下是一段用Python查找星座的示例代码:
```python
def find_constellation(month, day):
constellations = [
("水瓶座", "0101", "0218"), ("双鱼座", "0219", "0320"),
("白羊座", "0321", "0419"), ("金牛座", "0420", "0520"),
("双子座", "0521", "0621"), ("巨蟹座", "0622", "0722"),
("狮子座", "0723", "0822"), ("处女座", "0823", "0922"),
("天秤座", "0923", "1023"), ("天蝎座", "1024", "1122"),
("射手座", "1123", "1221"), ("摩羯座", "1222", "1231")
]
if month == 1 and day < 21 or month == 12 and day > 21:
return "摩羯座"
for constellation in constellations:
start_month = int(constellation[1][:2])
start_day = int(constellation[1][2:])
end_month = int(constellation[2][:2])
end_day = int(constellation[2][2:])
if month == start_month and day >= start_day or month == end_month and day <= end_day:
return constellation[0]
print(find_constellation(9, 23)) # 输出:天秤座
```
该代码中,将所有星座的日期范围存储在`constellations`列表中,然后根据输入的`month`(月份)和`day`(日期)判断所在星座。如果输入的日期早于1月21日或晚于12月21日,返回摩羯座。接着利用循环比较输入的日期和每个星座的开始和结束日期,返回对应的星座名称。