2014-02-24 28 views

回答

3

所有你需要的是平日的列表:

weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] 

然後用一天整數作爲索引:

def day_name(day): 
    return weekdays[day] 

這是可行的,因爲Python列表使用基於0的索引:

>>> weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] 
>>> weekdays[0] 
'Sunday' 
>>> weekdays[5] 
'Friday' 
+0

哦哇感謝非常有幫助!非常感謝你 –

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" 

我希望這是你在找什麼:)

+0

它的作品,但不是非常pythonic ... –

+1

雅你是對的,但我認爲這傢伙需要簡單的解決方案,以便他可以理解..那爲什麼我寫這個.. – Haroon

+0

公平不夠,你得到一個+1 :-) –

相關問題