python入门小程序python小程序:
实验目的:假设人一辈子活30000天,输入出生日期,计算死去的那一天。
实验过程:考虑的主要是年月日的逻辑控制以及python中函数传值的情况。另外熟悉python的date库。
实验总结:python的函数传值传的全是引用,类比于C语言也就是指针。如果传的是不可以变量的话(大部分变量都是不可变量,我知道的可变量只有列表List和字典Dictionary),那么如果在被调用函数内部改变了值的话会另外再新创一个变量,没有改变值的话就保持原来的变量。本例中,dead传到calumonth函数中,一开始...
python小程序:
实验目的:假设人一辈子活30000天,输入出生日期,计算死去的那一天。
实验过程:考虑的主要是年月日的逻辑控制以及python中函数传值的情况。另外熟悉python的date库。
实验
:python的函数传值传的全是引用,类比于C语言也就是指针。如果传的是不可以变量的话(大部分变量都是不可变量,我知道的可变量只有列表List和字典Dictionary),那么如果在被调用函数内部改变了值的话会另外再新创一个变量,没有改变值的话就保持原来的变量。本例中,dead传到calumonth函数中,一开始的dead和原来的dead是一个东西,后来改变了值就另外新建了一个叫dead的新对象,多以最后要return返回这个变量。
源码:
from datetime import date
def calumonth(dead,feb):
if dead.month == 2: #考虑闰月的情况
if dead.day == feb:
dead = dead.replace(month=3)
dead = dead.replace(day=1)
else :
newday = dead.day + 1
dead = dead.replace(day=newday)
elif dead.month == 1 or dead.month == 3 or dead.month==5 or dead.month==7 or
dead.month==8 or dead.month==10 or dead.month==12: #大月
if dead.day == 31:
if dead.month == 12:
newy = dead.year + 1
dead = dead.replace(year=newy)
dead = dead.replace(month=1)
dead = dead.replace(day=1)
else:
dead = dead.replace(day=1)
newmonth = dead.month + 1
dead = dead.replace(month=newmonth)
else:
newday = dead.day + 1
dead = dead.replace(day=newday)
else:
if dead.day == 30:
newmonth = dead.month + 1
dead = dead.replace(month=newmonth)
dead = dead.replace(day=1)
else:
newday = dead.day + 1
dead = dead.replace(day=newday)
return dead
def deadday(year,month,day):
bir = date(year,month,day)
dead = bir
while (dead-bir).days < 30000 :
if dead.month != 12:
newmonth = dead.month + 1
dead = dead.replace(month=newmonth) else:
newyear = dead.year + 1
dead = dead.replace(year=newyear)
dead = dead.replace(month=1)
if (dead-bir).days != 30000:
if dead.month != 1:
abstractmonth = dead.month - 1
dead = dead.replace(month=abstractmonth) else:
abyear = dead.year - 1
dead = dead.replace(year=abyear)
dead = dead.replace(month=12)
while (dead-bir).days != 30000 :
if dead.year % 100 == 0:
if dead.year % 400 == 0:
dead = calumonth(dead,29)
else:
dead = calumonth(dead,28)
elif dead.year % 4 == 0:
dead = calumonth(dead,29)
else:
dead = calumonth(dead,28)
if (dead-bir).days == 30000:
return dead
else:
return
print(deadday(1998,7,9)) #在此改变出生日期
本文档为【python入门小程序】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。