Traceback (most recent call last):
File "/Users/jorilee/Desktop/hello.py", line 30, in <module>
month_name = months[month_number-1]
IndexError: list index out of range
源代码:
#根据给定的年月日以数字形式打印出日期
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 = eval(input('Year: '))
month = eval(input('Month(1-12): '))
day = eval(input('Day(1-31): '))
month_number = int(month)
day_number = int(day)
#月份和天数减1,以获取正确的索引
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print(month_name + ' ' + ordinal + ', ' + year)
File "/Users/jorilee/Desktop/hello.py", line 30, in <module>
month_name = months[month_number-1]
IndexError: list index out of range
源代码:
#根据给定的年月日以数字形式打印出日期
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 = eval(input('Year: '))
month = eval(input('Month(1-12): '))
day = eval(input('Day(1-31): '))
month_number = int(month)
day_number = int(day)
#月份和天数减1,以获取正确的索引
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print(month_name + ' ' + ordinal + ', ' + year)