2014-12-02 98 views
0

我的任務是:編寫一個函數來計算星期幾

編寫一個函數,計算並傳回H中給出的月的年,月,日的一週內(整數)的日子。使用蔡勒公式

我的代碼:

DAYS = ["Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"] 

if month == 1: 
    month = 13 
    year -= 1 
elif month == 2: 
    month = 14 
    year -= 1 

century = str(year)[:1] 
century_year = str(year)[2:] 

century = int(century) 
century_year = int(century_year) 

h = (day_month+((26(month+1))//10+century_year)+(century_year//4)+(century//4)+(5*century))%7 

day = DAYS[h] 
return day 

當我試圖運行此我得到

h = (day_month+((26(month+1))//10+century_year)+(century_year//4)+(century//4)+(5*century))%7 
TypeError: 'int' object is not callable 
+9

'26(月+ 1)' - >'26 *(月+ 1。 )' – vaultah 2014-12-02 18:52:56

+0

謝謝,這解決了我的錯誤,但它沒有給我正確的日期,當我把它放在2014年12月2日,它應該給星期二,但它給星期五 – 2014-12-02 19:02:59

+0

@NicolasPagnotta:這是一個新問題,不要期待得到答案通過將後續評論添加到現有問題中。如果你想知道你的邏輯有什麼問題,寫一個新的問題來解釋你的代碼做錯了什麼以及你做了什麼來調試它。 – abarnert 2014-12-02 19:50:57

回答

1

我認爲錯誤可能是乘法。 當你編寫26(month + 1)時,Python會認爲你是以參數month + 1調用26的函數,你必須使用乘號 「*」

嘗試寫: H =(day_month +((26 *(月+ 1)...

+0

是的,謝謝你解決問題! – 2014-12-03 00:14:47

相關問題