wangdajiang0711
wangdajiang0711
2017-01-23 04:15
采纳率: 100%
浏览 1.0k

Python新手的一个序列小问题 求大神指导

具体代码如下:

# -*- coding: UTF-8 -*-

#根据给定的年月日打印出响应的日期

mouths = [

     "January"

     "February"

     "March"

     "April"

     "May"

     "June"

     "July"

     "August"

     "September"

     "October"

     "November"

     "December"

   ]

endings = ["st","nd","rd"]+17*["th"] \

      +["st","nd","rd"]+7*["th"]\

      +["st"]

year = raw_input("Year: ")

mouth = raw_input("Mouth(1-12): ")

day = raw_input("Day(1-31): ")

mouth_number = int(mouth)

day_number = int(day)

mouth_name = mouths[mouth_number-1]

ordinal = day + endings[day_number-1]

print mouth_name+' '+ordinal+','+year

个人感觉代码应该没问题,但是在运行这个程序的时候

============= RESTART: C:/Python27/My Python Programs/suoyin.py =============

Year: 1997

Mouth(1-12): 3

Day(1-31): 5

Traceback (most recent call last):

File "C:/Python27/My Python Programs/suoyin.py", line 34, in

mouth_name = mouths[mouth_number-1]

IndexError: list index out of range

一直有这个问题,求大神指导。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • strongerzhi
    Kolamu 2017-01-23 04:33
    已采纳

    好粗心,逗号没加!

     mouths = [
    
         "January",
    
         "February",
    
         "March",
    
         "April",
    
         "May",
    
         "June",
    
         "July",
    
         "August",
    
         "September",
    
         "October",
    
         "November",
    
         "December",
    
       ]
    
    点赞 评论

相关推荐