SyntaxError: Non-ASCII character '\xe6' in file ./1.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details


百度查了一下是因为Python在默认状态下不支持源文件中的编码所致。

在文件头部添加如下两行注释码:

 #!/usr/bin/env python

# vim: set fileencoding= : 例如,可添加# vim: set fileencoding=utf-8 :


以下是修正后完整代码:

#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
#根据给定的年月日以数字形式打印出日期
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 = raw_input('year: ')
month = raw_input('month(1-12): ')
day = raw_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