0
点赞
收藏
分享

微信扫一扫

适合新手入门的python程序(学习数据类型以及面向对象编程)


This is a program that can output the next day after the date of input. The original code is provided by 白宸. I am responsible for improving the code.

整个程序我将用类的方法完成,先给大家展示一下结构:

"""
This is a program that can output the next day after the date of input.
The original code is provided by 白宸.
I am responsible for improving the code.
"""
from datetime import datetime

class DATE(object):
def __init__(self, input_date):
self.input_date = input_date

def is_leap_year(self,year): #判断是否是闰年
pass

def split_date(self): #把年月日分开
pass

def count(self): #统计天数
pass

def main():
input_date = input('请输入日期(yyyymmdd):')
date = DATE(input_date) #实例化
date.count()

if __name__ == '__main__':
main()

在类的内部,使用 def 关键字来定义一个方法,与一般函数定义不同,类方法必须包含参数 self, 且为第一个参数,self 代表的是类的实例。

面向对象看似很难,但也可以很简单,我们先从最简单的开始写起:

class A():
def new(self):
pass

def main():
a = A()
a.new()

if __name__ == '__main__':
main()

可以把pass换成一句输出:

def new(self):
print("这是new函数")

以下是输出结果:

适合新手入门的python程序(学习数据类型以及面向对象编程)_算法


刚开始学习时,可以当成一种格式,先记下来,接触多了以后,大家可以在这个基础上继续加深难度,具体的可以看以下链接:

​​https://www.runoob.com/python3/python3-class.html​​接下来,开始分析一下程序,我们的目标是,输入年月日(如:20200204),能输出这一天是该年的第几天:

适合新手入门的python程序(学习数据类型以及面向对象编程)_小程序_02


分析一下,输出这8个数字,计算机是无法分辨年月日的,我们需要把这8位数拆分开,然后再通过程序计算。接下来,我们相当于把年、月、日这三个参数带入函数中,就可以得出最终的y,即总天数。

下面,我们开始编写函数:

def is_leap_year(self,year):
#判断是否是闰年
is_leap = False
if ( year % 400 == 0) or (( year % 4 == 0) and ( year % 100 != 0)):
is_leap = True
return is_leap

首先是判断闰年,因为公历中只分闰年和平年,平年有365天,而闰年有366天(2月中多一天),这里我们需要了解一下闰年是怎么定义的:

  • 普通闰年:公历年份是4的倍数的,且不是100的倍数,为闰年。(如2004年就是闰年);
  • 世纪闰年:公历年份是整百数的,必须是400的倍数才是世纪闰年(如1900年不是世纪闰年,2000年是世纪闰年);

有了理论基础就可以开始编写代码了:

普通闰年:
​​​python year % 4 == 0 and year % 100 != 0​

世纪闰年:
​​​python year % 400 == 0​

接下来是提取年月日:

def split_date(self):
date = datetime.strptime(self.input_date,"%Y%m%d")
#提取出年,月,日
year = date.year
# print("year:",year)
month = date.month
# print("month:",month)
day = date.day
# print("day:",day)
return year,month,day

这时,datetime这个资源库就派上用场了,如果对这个资源库不太了解的话,大家可以按住CTRL键,把鼠标左键放到datetime上,就可以查看代码了:

适合新手入门的python程序(学习数据类型以及面向对象编程)_编程语言_03


按住CTRL点击,其他的资源库也可以用同样的方法查看

适合新手入门的python程序(学习数据类型以及面向对象编程)_资源库_04


随后是核心的代码:

def count(self):
year, month, day = self.split_date()[0], self.split_date()[1], self.split_date()[2]
#初始化天数
if self.is_leap_year(year) and month > 2:
days = 1
else:
days = 0
#字典类型
days_inmonth_31dict = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
for i in range(1, month):
days += days_inmonth_31dict[i]
days += day
print('第{}年的第{}天'.format(year,days))

把年月日传回名为count的函数,调用判断闰年的函数,如果是闰年,切大于2月,那么初始天数为1;否则初始天数为0。

然后是比较重要的数据类型:字典

d = {key1 : value1, key2 : value2 }

字典的每个键值key对用冒号分割,每个对之间用逗号分割,整个字典包括在花括号{}中 。访问字典:

print (d[key1])

输出结果为value1

现在我们就可以看看这个代码了,先写好存放天数的字典:

days_inmonth_31dict = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}

解析一下,1月份有31天;2月份有28天,以此类推…

接下来,把传回的月份放到循环里:

for i in range(1, month):
days += days_inmonth_31dict[i]
days += day

从1月开始进行,到传回的month结束,如果month=1,就直接加天数;如果month=2,那就把1月份的31天加到days,再加上天数。如果觉得这块比较绕的话,可以打印一下days和day,注意这是两个变量,days是最后的总天数,day是提取出来的天,比如20200204,那么day就是4。

最后把主函数写上:

def main():
input_date = input('请输入日期(yyyymmdd):')
# print(input_date)
date = DATE(input_date)
date.count()

最后还有一句:

if __name__ == '__main__':
main()

这是程序的入口。以下就是全部代码,在这里分享给大家学习,如果有什么可以继续改进的,也欢迎大家在评论区里讨论:

"""
This is a program that can output the next day after the date of input.
The original code is provided by 白宸.
I am responsible for improving the code.
"""
from datetime import datetime

class DATE(object):
def __init__(self, input_date):
self.input_date = input_date

def is_leap_year(self,year):
#判断是否是闰年
is_leap = False
if ( year % 400 == 0) or (( year % 4 == 0) and ( year % 100 != 0)):
is_leap = True
return is_leap

def split_date(self):
date = datetime.strptime(self.input_date,"%Y%m%d")
#提取出年,月,日
year = date.year
# print("year:",year)
month = date.month
# print("month:",month)
day = date.day
# print("day:",day)
return year,month,day

def count(self):
year, month, day = self.split_date()[0], self.split_date()[1], self.split_date()[2]
#初始化天数
if self.is_leap_year(year) and month > 2:
days = 1
else:
days = 0
#字典类型
days_inmonth_31dict = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
for i in range(1, month):
days += days_inmonth_31dict[i]
days += day
print('第{}年的第{}天'.format(year,days))


def main():
input_date = input('请输入日期(yyyymmdd):')
# print(input_date)
date = DATE(input_date)
date.count()


if __name__ == '__main__':
main()


举报

相关推荐

0 条评论