我在程序方面遇到麻烦。所以我正在尝试编写一个返回一天名称的函数。 (例如:2014年1月1日是星期三)我严格只处理2014年。我将获得的投入是d = day,m = month。
这是我到目前为止所做的。
def day(d,m): # Function for determining day name for a given date.
"""Where m is an integer from 1 through 12 expressing a month, and d is an integer from
1 through 31 expressing the day-part of a date in 2014."""
if 1<=m<=12:
return
elif 1<=d<=31:
return ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
else:
return "Error"
答案 0 :(得分:2)
import datetime as DT
def day(d,m): # Function for determining day name for a given date.
"""Where m is an integer from 1 through 12 expressing a month, and d is an
integer from 1 through 31 expressing the day-part of a date in 2014."""
date = DT.date(2014, m, d) # 1
return date.strftime('%A')
如果2014/m/d
不是有效日期,则会引发ValueError。引发异常通常比返回"Error"
之类的字符串更好。