lsq546397641的个人博客分享 http://blog.sciencenet.cn/u/lsq546397641

博文

题目4

已有 1500 次阅读 2017-4-19 15:48 |系统分类:科研笔记|关键词:学者

输入某年某月某日,判断这一天是这一年的第几天?

import datetime

import time

dtstr = str(input('请输入日期(例:20150721):'))

dt = datetime.datetime.strptime(dtstr, "%Y%m%d")

another_dtstr =dtstr[:4] +'0101'

another_dt = datetime.datetime.strptime(another_dtstr, "%Y%m%d")

#print ("2017年已经度过:",int((dt-another_dt).days) + 1,"天")

a=dt-another_dt

print(dtstr)

print(dt)

print(another_dtstr)

print(another_dt)

print(a)

其他方法:

a=int(input("请输入年份:"))

b=int(input("请输入月份:"))

c=int(input("请输入几号:"))

b_31=[1,3,5,7,8,10,12]

b_30=[4,6,9,11]

#b_29or28=[2]

d=0

e=0

for i in range(1,b):

   if i in b_31:

       d+=1        

   elif i in b_30:

       e+=1

   else:

       pass

if a%4==0 and b>2:

   print(d*31+e*30+c+29)

elif a%4!=0 and b>2:

   print(d*31+e*30+c+28)

else:

   print(d*31+e*30+c)







https://m.sciencenet.cn/blog-994715-1049871.html


下一篇:题目3

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-19 22:52

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部