天天看點

python3的星期函數_使用Zeller的同餘Python3的星期幾

我剛開始學習計算機科學,對python3和一般的編碼非常陌生。我們的第一個任務是用Zellers同餘計算一周中的哪一天,我有幾個關于我所寫代碼的問題。在year = int(input("Year: "))

while not int(year) in range(1583, 10000):

year = input("Out of allowed range 1583 - 9999. Please enter a valid

number: ")

month = int(input("Month: "))

while not int(month) in range(1, 13):

month = input("Out of allowed range 1 - 12. Please enter a valid number: ")

if month == 1 or month == 2:

month += 12

year -= 1

day = int(input("Day: "))

while not int(day) in range(1, 32):

day = input("Out of allowed range 1 - 31. Please enter a valid

number: ")

result = ( day + 13 * (month+1) // 5 + year + year // 4

- year// 100 + year // 400 ) % 7

weekday = {0: "Saturday",1: "Sunday", 2: "Monday",3: "Tuesday",4:

"Wednesday",5: "Thursday",6: "Friday"}

print("The day is " + weekday[int(result)] + ".")

首先,我要確定日範圍與正确的月份相關。例如,當月份輸入為1、3、5、7、8、10或12時,日間隔應為1-31。如果月份輸入為4、6、9或11,則日間隔應為1-30。最後我還要考慮閏年。我不知道怎麼寫這些要求。在

而且,所有的批評都是受歡迎的。我知道這太不像話了,是以請讓我知道我能做得更好!在