I’ve written a code like that but it’s giving me an error. I shouldn’t use DateTime code by the way.
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.")
And the error is here:
Can anyone explain what is wrong with that code?
>Solution :
Seems like you made a typo here
class Tarih:
def init(self, d, m, y,s):
In python you should declare constructor with __init__ function
class Tarih:
def __init__(self, d, m, y,s):