其中计算星座有两种方法zodiac()和zodiac2(),虽然第一种很简洁,但是第二种更易懂。拿来主义,学习学习先。
在p2.x的版本中,unicode的字符串前都要添加u, 但在p3.x中,统一更名为str。所以前面的u就省略了。我用的是p3.1b1版。所以有些与p2.x不兼容的地方。
# Zodiac.py
def chinese_zodiac(year):
return "猴鸡狗猪鼠牛虎兔龙蛇马羊"[year%12]
def zodiac(month, day):
n = ('摩羯座','水瓶座','双鱼座','白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座')
d = ((1,20),(2,19),(3,21),(4,21),(5,21),(6,22),(7,23),(8,23),(9,23),(10,23),(11,23),(12,23))
s = filter(lambda y:y<=(month,day), d)
return n[len(list(s))%12] # object of type 'filter' has no len(), so convert 'filter' to 'list' by force
def zodiac2(month,day):
zodiac_map = {
'白羊座':[(3,21), (4,20)],
'金牛座':[(4,21), (5,20)],
'双子座':[(5,21), (6,21)],
'巨蟹座':[(6,22), (7,22)],
'狮子座':[(7,23), (8,22)],
'处女座':[(8,23), (9,22)],
'天秤座':[(9,23), (10,22)],
'天蝎座':[(10,23), (11,21)],
'射手座':[(11,23), (12,22)],
'水瓶座':[(1,20), (2,18)],
'双鱼座':[(2,19), (3,20)]
}
for k,v in zodiac_map.items():
if v[0] <= (month,day) <= v[1]:
return k
if (month,day) >= (12,22) or (month,day) <= (1,19):
return '摩羯座'
if __name__ == "__main__":
Birthday = input("Please input your birthday in the form. Year/Month/Day:")
year = int(Birthday.split('/')[0])
month = int(Birthday.split('/')[1])
day = int(Birthday.split('/')[2])
chineseZodiac = chinese_zodiac(year)
StartZodiac = zodiac2(month, day)
print("Your chinese zodiac is: %s \n and your zodiac is: %s " %(chineseZodiac, StartZodiac))