用python查找星座
时间: 2023-05-27 15:04:04 浏览: 194
星座.py
请问具体需要哪方面的星座相关信息?
如果您是想查找某个日期的星座,可以使用以下代码:
```python
from datetime import datetime
def get_zodiac(month, day):
# 判断月份和日期是否合法
if not (1<=month<=12 and 1<=day<=31):
return None
else:
# 根据日期获取星座
if month == 1:
return 'Capricorn' if day <= 20 else 'Aquarius'
elif month == 2:
return 'Aquarius' if day <= 19 else 'Pisces'
elif month == 3:
return 'Pisces' if day <= 20 else 'Aries'
elif month == 4:
return 'Aries' if day <= 20 else 'Taurus'
elif month == 5:
return 'Taurus' if day <= 21 else 'Gemini'
elif month == 6:
return 'Gemini' if day <= 21 else 'Cancer'
elif month == 7:
return 'Cancer' if day <= 22 else 'Leo'
elif month == 8:
return 'Leo' if day <= 23 else 'Virgo'
elif month == 9:
return 'Virgo' if day <= 23 else 'Libra'
elif month == 10:
return 'Libra' if day <= 23 else 'Scorpio'
elif month == 11:
return 'Scorpio' if day <= 22 else 'Sagittarius'
elif month == 12:
return 'Sagittarius' if day <= 21 else 'Capricorn'
# 测试代码
month = 10
day = 1
zodiac = get_zodiac(month, day)
if zodiac:
print(f"The zodiac sign for {month}/{day} is {zodiac}")
else:
print(f"Invalid date: {month}/{day}")
```
输出结果为:
```
The zodiac sign for 10/1 is Libra
```
如果您是想查找某个星座的相关信息,可以使用第三方库 `pyhoroscope`:
```python
from pyhoroscope import Horoscope
from pyhoroscope import symbols
# 获取天蝎座的今日运势
scorpio = Horoscope(symbols.Scorpio)
print(scorpio.today())
```
输出结果为:
```
If you have some extra money, Scorpio, this is a good day to invest. ...
强调:此 Api 非官方Api,不保证 100% 准确。
阅读全文