我必须作为一个赋值语句来编写代码,如果输入日期不是,则返回一个带有'*'的输入列表的相同长度的列表格式正确(gg-mm-yyyy),否则返回月份缩写(例如11月份的nov)。输入由检查程序给出。 如果我没有解释得很好有一个例子试图在不同类型的列表中的所有元素上使用len
输入列表 - >[1,'10-10-2007', '2-3-1962','home', '31-11-2007']
输出列表 - >['*', 'october', '*', '*', '*']
现在,这里是我的问题。我试图运行这样的事情
months=[1,'10-10-2007', '2-3-1962','casa', '31-11-2007']
b=['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
c=0
d=10
if len(months[c]) == d:
z+=1
if months[c][3] == 1:
e=10+months[c][4]
c=c+1
else e=months[c][4]
c=c+1
else
c=c+1
print('*')
print(b[e-1])
现在我得到的错误是
TypeError: object of type 'int' has no len()
,我已经明白,int
不能有长度。但是,我怎样才能将int
翻译成可能有长度的东西?
您的示例输出使用* full * month名称'october',但您的代码使用缩写。在我的回答中,我使用了'calendar.month_abbr'来提供缩写的月份名称,但是如果您需要*全部*月份名称,只需使用'_name'对'_abbr'部分进行重新设置即可使用['calendar.month_name'] ://docs.python.org/3/library/calendar.html#calendar.month_name)。你可能想澄清你需要什么。 –