我試圖用這個 python 程式使用 Anonymous Gregorian Computus Algorithm 計算復活節在特定年份的日期,但我一直收到一個錯誤。我的代碼塊輸出正確的結果,但我發現很難將“st”附加到“st”串列中的數字。
# Read the input from the user
year = int(input("Please enter a year to calculate the date of Easter: "))
a = year % 19
b = year // 100
c = year % 100
d = b // 4
e = b % 4
f =( b 8) // 25
g = (b - f 1) // 3
h = ((19 * a) b - d - g 15) % 30
i = c // 4
k = c % 4
l = (32 (2 * e) (2 * i) - h - k) % 7
m = (a (11 * h ) (22 * l)) // 451
month = (h l - (7 * m) (114)) // 31
day = 1 (( h l - (7 * m) (114)) % 31)
st = ['1', '21', '31']
nd = ['2', '22']
rd = ['3', '23']
th = ['4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '24', '25', '26', '27', '28', '29', '30']
if day in st:
print("In the year", year, "Easter will fall on the", str(day) "st", "of", month)
elif day in nd:
print("In the year", year, "Easter will fall on the", str(day) "nd", "of", month)
elif day in rd:
print("In the year", year, "Easter will fall on the", str(day) "rd", "of", month)
else:
print("In the year", year, "Easter will fall on the", str(day) "th", "of", month)
使用這個演算法,我計算了不同年份來檢查我的代碼塊是否正確,但是當我計算像 2024 年和 2040 年這樣復活節日期落在“st”串列中的日子時,我一直得到 '31th 和第 1 次,而不是第 31 次和第 1 次。我不知道我缺少哪些代碼行。如果您能幫助我或告訴我如何最好地計算它,我將非常感激。
PS:我已經搜索過其他類似的問題,但我解決的問題是完全不同的語言,我沒有找到使用匿名公歷計算演算法來計算復活節日期的問題。
謝謝你。
uj5u.com熱心網友回復:
您可以使用 Python 解釋器輕松查看問題:
$ python3
>>> st = ['1', '21', '31']
>>> 21 in st
False
>>> '21' in st
True
字串和數字是不同的東西。您計算一個數字,因此您的測驗必須使用數字。
$ python3
>>> st = [1, 21, 31]
>>> 21 in st
True
您應該使用集合而不是串列進行包含測驗。在這種情況下,它并沒有太大的區別,但這是一個好習慣,以防有很多值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460394.html
