【发布时间】:2021-02-28 15:59:44
【问题描述】:
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
嗯,类似的。感谢你 我已经尝试了一些其他的东西,但它也没有工作,这段代码工作了一半
【问题讨论】:
-
提示:使用字典
-
代码似乎与您描述的相反 - 它需要一个月的字符串并将其转换为数字......
-
-
但恰恰相反。你能更清楚你的问题吗?我的意思是,就像你做的那样
if month == 'December': num_month = '12; Season: Winter'你不能把它改成if month == 12: num_month = 'December; Season: Winter'吗? -
如果我问错了,我很糟糕,我是堆栈的新手,但我认为你提出了一个很好的观点,我可以改变它。谢谢你
标签: python