python比较月日_Python:輸入年月日判斷是此年的第多少天

方法一:

#!\usr\bin\python

# coding=utf-8

year = int(raw_input("year:"))

month = int(raw_input("month:"))

day = int(raw_input("day:"))

the_month1 = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]#平年

the_month2 = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]#閏年

if ((year % 4 == 0)and(year % 100 != 0))or(year % 400 == 0):

sumday = the_month2[month-1]+day

else:

sumday = the_month1[month-1]+day

print "這是第 %d 天"%sumday

結果:

E:\Python27\python.exe E:/PycharmProjects/file/guesstheday.py

year:2015

month:6

day:7

這是第 158 天

Process finished with exit code 0

方法二:

#!\usr\bin\python

# coding=utf-8

year = int(raw_input("year:"))

month = int(raw_input("month:"))

day = int(raw_input("day:"))

the_month1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]#平年

sumday = day

for m in range(0, month-1):

sumday = sumday+the_month1[m]

if ((year % 4 == 0)and(year % 100 != 0))or(year % 400 == 0):

if(month>2):#此處要考慮若輸入的月份沒有超過2,就不用加1

sumday=sumday+1

print "這是第 %d 天" % sumday

else:

print "這是第 %d 天" % sumday

else:

print "這是第 %d 天"%sumday

方法三:

#!\usr\bin\python

# coding=utf-8

import time

D = raw_input("請輸入年份:格式為YYYY-MM-DD")

d = time.strptime(D, '%Y-%m-%d').tm_yday

print "the {} day of this year!".format(d)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值