Python题目 大学作业题..

键盘读入 name, birth_year, birth_month, birth_day。这三个变量分别代表你的姓名, 出生年与出生月份。程序将打印:'你好, name, 你 x 岁了,你是 constellation。',其中 constellation 是指这个出生年月对应的星座。此外,这是个多轮对话,要求能够多次读入(注 意,不是多次运行程序,而是运行一次程序就可以做到这点)。当 birth_year 等于-1 时程 序退出,退出时打印:'再见!'。

你题目的解答代码如下:

import datetime

#计算星座
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))
    return n[len(list(filter(lambda y:y<=(month,day), d)))%12]

while True:
    name = input("请输入姓名:")
    birth_year, birth_month, birth_day = input("请输入出生年 月 日(以空格分隔):").split(" ")
    birth_year = int(birth_year)
    birth_month = int(birth_month)
    birth_day = int(birth_day)
    if birth_year == -1:
        break
    today = datetime.date.today()
    x = today.year - birth_year
    constellation = Zodiac(birth_month,birth_day)
    print(f"你好, {name}, 你 {x} 岁了,你是 {constellation}。")
print("再见!")

如有帮助,望采纳!谢谢!