雷达upupup 2017-01-23 04:15 采纳率: 100%
浏览 1056
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • Kolamu 2017-01-23 04:33
    关注

    好粗心,逗号没加!

     mouths = [
    
         "January",
    
         "February",
    
         "March",
    
         "April",
    
         "May",
    
         "June",
    
         "July",
    
         "August",
    
         "September",
    
         "October",
    
         "November",
    
         "December",
    
       ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!