一个我无法解决的问题,是如何编写一个函数day_name
,它将0-6的整数转换为星期日,星期一,0为星期日。
我觉得这很有帮助,但是年月和月份让我感到困惑= which day of week given a date python
答案 0 :(得分:3)
您只需要一个工作日列表:
weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
然后使用day integer作为索引:
def day_name(day):
return weekdays[day]
这是有效的,因为Python列表使用基于0的索引:
>>> weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
>>> weekdays[0]
'Sunday'
>>> weekdays[5]
'Friday'
答案 1 :(得分:1)
嗨,这是你的功能
def dayOfTheWeek(n):
if n == 0:
return "Sunday"
elif n == 1:
return "Monday"
elif n == 2:
return "Tuesday"
elif n == 3:
return "Wednesday"
elif n == 4:
return "Thursday"
elif n == 5:
return "Friday"
elif n == 6:
return "Saturday"
else:
return "Invalid Number"
我希望那就是你要找的东西:)