python判断输入日期为第几天的实例

yipeiwu_com7年前Python基础

如下所示:

# -*- coding: utf-8 -*-
# 简述:要求输入某年某月某日
# 提问:求判断输入日期是当年中的第几天?
def which_day(year,month,day):
  list=[31,28,31,30,31,30,31,31,30,31,30,31]
  whichday=0
  if (year%4)==0 and (year%100)!=0 or (year%400)==0:
    list[1]=29
  for i in range(1,month):
    if month == 1:
      print day
    whichday=whichday+list[i-1]
  whichday=whichday+day
  print whichday
 
if __name__ == "__main__":
  year=int(raw_input("请输入年份:"))
  month=int(raw_input("请输入月份:"))
  day=int(raw_input("请输入天:"))
  which_day(year, month, day)

运行结果:

请输入年份:2017
请输入月份:3
请输入天:31
90

总结:

1、闰年的判断为能被4整除,但不能被100整除,或者能被400整除

刚开始三者都写成了and

2、关于list[i-1]刚开始写成了i-2,对于某些时期恰好是正确的,是因为成了list[-1]时从结尾开始取值

以上这篇python判断输入日期为第几天的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python使用pyserial进行串口通信的实例

安装pyserial pip install pyserial 查看可用的端口 # coding:utf-8 import serial.tools.list_ports...

python各类经纬度转换的实例代码

python各类经纬度转换的实例代码

python各类经纬度转换,具体代码如下所示: import math import urllib import json x_pi = 3.14159265358979324 *...

Python2.x利用commands模块执行Linux shell命令

用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands...

使用CodeMirror实现Python3在线编辑器的示例代码

使用CodeMirror实现Python3在线编辑器的示例代码

一、编写页面 主要是引入相关的css文件和js文件,这里采用简单插入link和script标签的形式。 <!DOCTYPE html> <html lang="en...

pandas 对group进行聚合的例子

如下所示: DataFrameGroupBy.agg(arg, *args, **kwargs) 例子: >>> df = pd.DataFrame({'A...