python - 我如何将 1-12 之间的数字转换为月份并给出季节名称(Pyhon)
问题描述
class Monthes:
def init(self,month):
self.month = month.lower()
def numb_month(self):
month = self.month
num_month = ''
if month == 'December':
num_month = '12; Season: Winter'
elif month == 'January':
num_month = '1; Season: Winter'
elif month == 'February':
num_month = '2; Season: Winter'
elif month == 'March':
num_month = '3 Season: Spring'
elif month == 'April ':
num_month = '4 Season: Spring'
elif month == 'May ':
num_month = '5 Season: Spring'
elif month == 'June':
num_month = '6 Season: Summer'
elif month == 'July':
num_month = '7 Season: Summer'
elif month == 'August':
num_month = '8 Season: Summer'
elif month == 'September':
num_month = '9 Season: Autumn'
elif month == 'October':
num_month = '10 Season: Autumn'
elif month == 'November':
num_month ='11; Season: Autumn'
return num_month
n = input("import month: ")
p =Monthes(n)
print(p.numb_month())
我想将 1-12 之间的数字转换为月份,例如
input: 3
outuput: March, Spring
以及类似的东西。谢谢你我已经尝试了一些其他的东西,但它也没有工作,这段代码工作但有一半
解决方案
将此用作您的代码:
def numb_month(m):
sessions = {"1":"January, Winter", "2":"February, Winter", "3":"March, Winter",\
"4":"April , Sprint", "5":"May , Sprint", "6":"June , Sprint", \
"7":"July, Summer", "8":"August, Summer", "9":"September, Summer", \
"10":"October, Autumn", "11":"November, Autumn", "12":"December, Autumn"}
return (sessions[m])
m = input("import month: ")
print(numb_month(m))
推荐阅读
- c++ - 使用 updatePaintNode 在 QT/OpenGL 中渲染视频:新帧仅在我调整屏幕大小时出现
- sql - 使用评论计数器对新闻进行单一查询
- javascript - 如何在 TestCafe 的夹具之间使用窗口变量?
- android - 收听消息时附近会阻止语音识别意图
- javascript - 如何确保特定节点已加载并可使用 javascript/jquery?
- mysql - 将 Wordpress 数据库导入 Laravel Valet,未导入行
- python - Sqlalchemy:将字典转换为布尔表达式
- javascript - 如何从 mapstatetoprops 访问反应路由器匹配对象
- sql - 将我的计算转换为用户定义的函数
- sql-server - SQL Server - 使用可用性组侦听器查询以获取服务器的 IP 地址