Python学习之根据日期判断日期在一年中的第几天
1、闰年判断
2、根据日期判断日期在一年中的第几天
运用
1、if语句使用
2、控制台输入
3、函数声明与调用
闰年判断
判断闰年的方法:
1、能被4整除,但不能被100整除
2、能被400整除
def isrunnian(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True;
else:
return False;
根据日期判断日期在一年中的第几天
判断一天是这一年中的第几天:
1、先判断这个月之前有多少天
2、把这个月之前的总天数加上这一天的天数
3、当月份大于3时考虑是否是闰年(多加一天)
def isday(year,month,day):
sum = 0;
num = 0;
months = [0,31,59,90,120,151,181,212,243,273,304,334];
if 0<=month<=12:
sum = months[month-1]
else:
print('Error Of The Month');
sum += day;
if isrunnian(year) == True:
num = 1;
if month > 2 and num == 1:
sum += 1;
return sum;
完整代码
"""
判断闰年的方法:
1、能被4整除,但不能被100整除
2、能被400整除
"""
def isrunnian(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True;
else:
return False;
"""
判断一天是这一年中的第几天:
1、先判断这个月之前有多少天
2、把这个月之前的总天数加上这一天的天数
3、当月份大于3时考虑是否是闰年(多加一天)
"""
def isday(year,month,day):
sum = 0;
num = 0;
months = [0,31,59,90,120,151,181,212,243,273,304,334];
if 0<=month<=12:
sum = months[month-1]
else:
print('Error Of The Month');
sum += day;
if isrunnian(year) == True:
num = 1;
if month > 2 and num == 1:
sum += 1;
return sum;
if __name__=='__main__':
year = int(input('Please input the year:'));
month = int(input('Please input the month:'));
day = int(input('Please input the day:'));
print('The day is the %d day in this year'%isday(year,month,day))