雷达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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动