from time import *
class Student:
def __init__(self, name, studentNum, birth):
self.name = name
self.studentNum = studentNum
self.birth = birth
#a function to find your age
def age(self):
b = self.birth.split('.')
y = int (b[0])
m = int (b[1])
d = int (b[2])
#get the current time in tuple format
a=gmtime()
#difference in day
dd=a[2]-d
#difference in month
dm=a[1]-m
#difference in year
dy=a[0]-y
#checks if difference in day is negative
if dd<0:
dd=dd+30
dm=dm-1
#checks if difference in month is negative when difference in day is also negative
if dm<0:
dm=dm+12
dy=dy-1
#checks if difference in month is negative when difference in day is positive
if dm<0:
dm=dm+12
dy=dy-1
print ("你目前是: %s 岁 %s 月 & %s 天"%(dy,dm,dd))
def displayStudent(self):
print ("Name : ", self.name, ", 学号: ", self.studentNum, ", 生日: ", self.birth)
"创建 Student 类的一个对象"
student = Student("王大明", 150032, "1999.05.06")
student.displayStudent()
student.age()
输出:
Name : 王大明 , 学号: 150032 , 生日: 1999.05.06
你目前是: 20 岁 0 月 & 23 天