【发布时间】:2022-01-25 19:50:28
【问题描述】:
我已经编写了这样的代码,但它给了我一个错误。顺便说一句,我不应该使用 DateTime 代码。
aylar = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def countLeapYears(d):
years = d.y
if (d.m <= 2):
years -= 1
ans = int(years / 4)
ans -= int(years / 100)
ans += int(years / 400)
return ans
def getDifference(dt1, dt2):
n1 = dt1.y * 365 + dt1.d
for i in range(0, dt1.m - 1):
n1 += aylar[i]
n1 += countLeapYears(dt1)
n2 = dt2.y * 365 + dt2.d
for i in range(0, dt2.m - 1):
n2 += aylar[i]
n2 += countLeapYears(dt2)
return (n2 - n1)
class Tarih:
def _init_(self, d, m, y,s):
self.d = d
self.m = m
self.y = y
self.s = s
tarih1 = Tarih(1, 12, 2021,0)
tarih2 = Tarih(4, 12, 2021,23)
farkgun = getDifference(tarih1,tarih2)
if (tarih1.s > tarih2.s):
farkgun = farkgun-1
tarih2.s = tarih2.s+24
print("Iki tarih arasindaki fark: ",farkgun,"gun.")
print("Iki tarih arasindaki saat: ",(farkgun*24)+(tarih2.s-tarih1.s),"saat.")
错误就在这里:
Traceback (most recent call last):
File "main.py", line 28, in <module>
ratih1 = Tarih(1, 12, 2021, 0)
TypeError: Tarih() takes no arguments
谁能解释这段代码有什么问题?
【问题讨论】:
-
始终将完整的错误回溯包含为 文本,而不是图像。
-
错字:
__init__缺少下划线。 -
我尝试将其写为文本,但在共享时出现错误,因为它将其视为代码。我必须添加一张图片。
-
“shoudn't”不是“mustn't”,所以我还是会使用它。
标签: python python-3.x date