python 新手求指导习题.#根据给定的年月日以数字形式打印出日期months = ['January''February''March''April''May''June''July''August''September''October''November''December']#以1~31的数字作为结尾的列表endingds = ['st','n
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/29 00:30:52
python 新手求指导习题.#根据给定的年月日以数字形式打印出日期months = ['January''February''March''April''May''June''July''August''September''October''November''December']#以1~31的数字作为结尾的列表endingds = ['st','n
python 新手求指导习题.
#根据给定的年月日以数字形式打印出日期months = [
'January'
'February'
'March'
'April'
'May'
'June'
'July'
'August'
'September'
'October'
'November'
'December'
]
#以1~31的数字作为结尾的列表
endingds = ['st','nd','rd'] + 17 * ['th']\
+['st','nd' ,'rd'] + 7 * ['th']\
+['st']
year = input('Year:')month = input('Moth (1-12):')
day = input ('Day(1-31):')
month_number = int(month)day_number = int(day)
#记得要将月份和天数减一,以获得正确的引索month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print(month_name + ' ' +ordinal + ',' + year)
最后在运行时出现了这样的错误提示,怎么改.我用的是3.3,
Year:2012
Moth (1-12):5
Day(1-31):18
Traceback (most recent call last):
File "E:/python/日历表.py",line 29,in
month_name = months[month_number-1]
IndexError:list index out of range
>>>
python 新手求指导习题.#根据给定的年月日以数字形式打印出日期months = ['January''February''March''April''May''June''July''August''September''October''November''December']#以1~31的数字作为结尾的列表endingds = ['st','n
超出索引了,month_number-1 已经不在months这个list的索引范围了,3.3我没用过,但是看你的months写个估计有问题,难道3.3支持list里面不加逗号?估计你在每个月份后面加个逗号就好了……起码这里能过去,后面过不过得去就不好说了,我用的2.7.3
给你改了下整体的代码
#根据给定的年月日以数字形式打印出日期
months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
#以1~31的数字作为结尾的列表
endings = ['st', 'nd', 'rd'] + 17 * ['th']\
+['st', 'nd' ,'rd'] + 7 * ['th']\
+['st']
year = input('Year: ')
month = input('Moth (1-12): ')
day = input ('Day(1-31): ')
month_number = int(month)
day_number = int(day)
#记得要将月份和天数减一,以获得正确的引索
month_name = months[month_number-1]
ordinal = str(day) + endings[day_number-1]
print(month_name + ' ' +ordinal + ',' + str(year))
其实上面输入的day、month和year已经是int类型了,不用再转换了,下面拼接字符串要注意类型,int类型不能和str类型直接拼接,要么转成str,要么格式化输出'%s %s %s' % (year,month,day)