根据字符串查找数字对应的月份

求助

要求根据数字1-12数字返回月份名称

monthstr = "一月二月三月四月五月六月七月八月九月十月十一月十二月"

monthid = eval(input("请输入月份数字(1-12):"))

if monthid in range(1,10):
    pos = (monthid - 1) * 2

    print(monthstr[pos:pos+2])
elif monthid in [11]:
    pos = (monthid - 1) * 2
    print(monthstr[pos:pos+3])
elif monthid in [12]:
    pos = (monthid - 1) * 2
    print(monthstr[pos+1:pos+4])
else:
    print("请输入正确的数字")

请教一下更简洁的代码示例,感谢

原创文章 27 获赞 34 访问量 2622

猜你喜欢

转载自blog.csdn.net/weixin_46313446/article/details/105334575
今日推荐