当前位置: 代码迷 >> 综合 >> Python根据出生日期判断你的星座
  详细解决方案

Python根据出生日期判断你的星座

热度:72   发布时间:2024-02-12 14:45:10.0

可以先查询星座和日期表格

   星座 出生日期(公历)
摩羯座

12月22日~1月19日

水瓶座 1月20日~2月18日
双鱼座 2月19日~3月20日
白羊座 3月21日~4月19日
金牛座 4月20日~5月20日
双子座 5月21日~6月21日
巨蟹座 6月22日~7月22日
狮子座 7月23日~8月22日
处女座 8月23日~9月22日
天秤座 9月23日~10月23日
天蝎座 10月24日~11月22日
射手座 11月23日~12月21日

 每个星座时间之间都有转折,通过这一点来进行推导

sdate = [20,19,21,20,21,22,23,23,23,24,23,22]
counts = ['摩羯座','水瓶座','双鱼座','白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座']

输入 

birth = input('请输入您的生日,格式为:2020-01-22或2020-1-22\n').strip(' ')

 将日期拆分

cbir = birth.split('-')
cmonth = str(cbir[1])
cdate = str(cbir[2])

 def自定义星座函数

判断cdate和sdate之间大小关系

def sign(cmonth,cdate):if int(cdate) < sdate[int(cmonth)-1]:print(counts[int(cmonth)-1])else:print(counts[int(cmonth)])

调用星座判断程序

sign(cmonth,cdate)

也可以加入星座符号哦!

  相关解决方案