python练习4
class Cexception :def __init__(self,year,month,day):
if Cexception.judge(year,month,day) :
self.year = year
self.month = month
self.day = day
else:
self.year = -1
self.month = -1
self.day = -1
print("日期有误")
def setTime(self,year,month,day):
if Cexception.judge(year,month,day) :
self.year = year
self.month = month
self.day = day
else:
self.year = -1
self.month = -1
self.day = -1
print("日期有误")
def display(self):
if self.month ==-1 or self.year == -1 or self.day == -1:
print("",end="")
else:
print(str(self.month)+"/"+str(self.day)+"/"+str(self.year))
def judge(year,month,day):
leap = False
legal = False
if year>0:
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
leap = True
if month in [1, 3, 5, 7, 8, 10, 12]:
if 1 <= day <= 31:
legal = True
elif month in [4, 6, 9, 11]:
if 1 <= day <= 30:
legal = True
elif month == 2:
if not leap and 1 <= day <= 28:
legal = True
elif leap and 1 <= day <= 29:
legal = True
else:
legal = False
return legal
c = Cexception(-1,11,2)
c.display()
c = Cexception(2019,11,2)
c.display()
c = Cexception(2019,2,29)
c.display()
c = Cexception(2020,2,29)
c.display()
标签:Cexception,python,self,练习,legal,month,year,day From: https://www.cnblogs.com/yunbianshangdadun/p/17404330.html