GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — Python 练习实例4 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
Python 基础教程
Python 基础教程 Python 简介 Python 环境搭建 Python 中文编码 Python VS Code Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 Python 循环语句 Python While 循环语句 Python for 循环语句 Python 循环嵌套 Python break 语句 Python continue 语句 Python pass 语句 Python Number(数字) Python 字符串 Python 列表(List) Python 元组 Python 字典(Dictionary) Python 日期和时间 Python 函数 Python 模块 Python 文件I/O Python File 方法 Python 异常处理 Python OS 文件/目录方法 Python 内置函数

Python 高级教程

Python 面向对象 Python 正则表达式 Python CGI 编程 Python MySQL Python 网络编程 Python SMTP Python 多线程 Python XML 解析 Python GUI 编程(Tkinter) Python2.x 与 3​​.x 版本区别 Python IDE Python JSON Python AI 绘画 Python 100例 Python 测验
Python 练习实例3
Python 100例

Python 练习实例4

Python 100例 Python 100例

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

程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天:

程序源代码:

实例

#!/usr/bin/python3 year = int(input('year:\n')) month = int(input('month:\n')) day = int(input('day:\n')) months = (0,31,59,90,120,151,181,212,243,273,304,334) if 0 < month <= 12: sum = months[month - 1] else: print ('data error') sum += day leap = 0 if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)): leap = 1 if (leap == 1) and (month > 2): sum += 1 print ('it is the %dth day.' % sum)

以上实例输出结果为:

year:
2015
month:
6
day:
7
it is the 158th day.

Python 100例 Python 100例

AI 思考中...
Python 练习实例3
Python 100例

19 篇笔记 写笔记

  1. #0

       ym

      853***[email protected]

    49

    参考解法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    year=int(raw_input("年:\n"))
    month=int(raw_input("月:\n"))
    day=int(raw_input("日:\n"))
    months1=[0,31,60,91,121,152,182,213,244,274,305,335,366] #闰年
    months2=[0,31,59,90,120,151,181,212,243,273,304,334,365] #平年
    
    if ((year%4==0)and(year%100!=0)) or((year%100==0)and(year%400==0)):
        Dth=months1[month-1]+day
    else:
        Dth=months2[month-1]+day
    print "是该年的第%d天"%Dth
    
    ym

       ym

      853***[email protected]

    10年前 (2017-04-11)
  2. #0

       苹果pai

      646***[email protected]

    20

    闰年需要同时满足以下条件:

    • 1、年份能被4整除;
    • 2、年份若是 100 的整数倍的话需被400整除,否则是平年。
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    # 输入任意年月日,知道是改年第几天
    
    p = [31,28,31,30,31,30,31,31,30,31,30,31] # 平年
    w = [31,29,31,30,31,30,31,31,30,31,30,31] # 闰年
    
    year =int(raw_input("请输入年:"+'\n'))
    month =int(raw_input("请输入月:"+'\n'))
    day=int(raw_input("请输入日:"+'\n'))
    
    arr=[31,28,31,30,31,30,31,31,30,31,30,31]
    sum=day
    for i in range(0,month-1):
        sum+=arr[i]
    if year%4==0:
        if year%100==0 and year%400!=0:
            print "这是今年的第%d天" % sum
        else:
            sum=sum+1
            print "这是今年的第%d天" % sum
    else: 
        print "这是今年的第%d天" % sum
    
    苹果pai

       苹果pai

      646***[email protected]

    10年前 (2017-04-13)
  3. #0

       流年细雨

      758***[email protected]

    15

    参考解法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    # 输入任意年月日,知道是改年第几天
    
    p = [31,28,31,30,31,30,31,31,30,31,30,31] # 平年
    w = [31,29,31,30,31,30,31,31,30,31,30,31] # 闰年
    
    year =int(raw_input("年:\n"))
    month =int(raw_input("月:\n"))
    day =int(raw_input("日:\n"))
    
    # 判断闰年,平年
    
    if year % 100 == 0:
        if year % 400 == 0:
            d=w
        else:
            d=p
    else:
        if year % 4 == 0:
            d=w
        else:
            d=p
    
    # 计算天数
    
    days = sum(d[0:month-1])+day
    print "%d.%d.%d是%d年的第%s天。"%(year,month,day,year,days)
    
    流年细雨

       流年细雨

      758***[email protected]

    10年前 (2017-04-13)
  4. #0

       Atom

      tum***@126.com

    9

    参考解法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    year = int(input('请输入年份:'))
    month = int(input('请输入月份:'))
    day = int(input('请输入日期:'))
    total = 0
    if year%4 == 0:
        days = 29
    else:
        days = 28
    if year%4 == 0:
        print year, '年是润年,2月有', days, '天!'
    else:
        print year, '年是平年,2月有', days, '天!'
    if month <= 1:
        total = day
    elif month == 2:
        total = 31 + day
    elif month == 9 or month == 11:
        total = (month - 2) * 30 + days + month/2 + day + 1
    else:
        total = (month - 2) * 30 + days + month/2 + day
    print year, '年',month, '月', day, '日,是这一年的第', total, '天!'
    
    Atom

       Atom

      tum***@126.com

    10年前 (2017-04-14)
  5. #0

       老虎头

      sha***[email protected]

    8

    参考解法:

    #!/usr/bin/python
    
    def isLeapYear(a):
        if (0 == a%4 or 0 == a%400) and 0 != a%100 :
            return 1
        else:
            return 0
    dict = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
    a = int(input("Input year:"))
    b = int(input("Input month:"))
    c = int(input("Input day:"))
    m = 0
    k = isLeapYear(a)
    for i in range(1,b):
        m = m + dict[i]
    m = m + isLeapYear(a) + c
    
    print(m)
    
    老虎头

       老虎头

      sha***[email protected]

    10年前 (2017-04-25)
  6. #0

       shusihui

      511***[email protected]

    12

    参考方法:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    year = int(raw_input('year:\n'))
    month = int(raw_input('month:\n'))
    day = int(raw_input('day:\n'))
    days = [31,28,31,30,31,30,31,31,30,31,30,31]
    if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
        days[2] += 1
    
    now = sum(days[0:month-1])+day
    print 'it is the %dth day.' %now
    
    shusihui

       shusihui

      511***[email protected]

    10年前 (2017-04-27)
  7. #0

       lqy126

      412***[email protected]

    9

    参考方案:

    #!/usr/bin/env python
    #coding:utf-8
    
    import time
    import sys
    
    reload(sys)
    sys.setdefaultencoding('utf-8')  # 设置 'utf-8'  
    a = raw_input("输入时间(格式如:2017-04-04):")
    t = time.strptime(a,"%Y-%m-%d")
    print time.strftime("今年的第%j天",t).decode('utf-8')
    lqy126

       lqy126

      412***[email protected]

    10年前 (2017-05-05)
  8. #0

       初学者

      646***[email protected]

    17

    参考方法:

    #! /usr/bin/env python
    # coding:utf-8
    
    import time
    
    D=raw_input("请输入年份,格式如XXXX-XX-XX:")
    d=time.strptime( D,'%Y-%m-%d').tm_yday
    print "the {} day of this year!" .format(d)
    初学者

       初学者

      646***[email protected]

    10年前 (2017-05-11)
  9. #0

       cc

      123***3.123

    8

    Python3 参考解法:

    #!/usr/bin/python3
    
    date = input("输入年月日(yyyy-mm-dd):")
    y,m,d = (int(i) for i in date.split('-'))
    sum=0
    special = (1,3,5,7,8,10)
    for i in range(1,int(m)):
        if i == 2:
            if y%400==0 or (y%100!=0 and y%4==0):
                sum+=29
            else:
                sum+=28
        elif(i in special):
            sum+=31
        else:
            sum+=30
    sum+=d
    print("这一天是一年中的第%d天"%sum)
    cc

       cc

      123***3.123

    10年前 (2017-06-03)
  10. #0

       半音节

      104***[email protected]

    8

    真正意义上自己思考写出来的第一题,撒花*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    i= int(input('请输入年份:'))
    j= int(input('请输入月份:'))
    k= int(input('请输入天数:'))
    month = [31,28,31,30,31,30,31,31,30,31,30,31]
    day = 0
    if i%4 == 0  or i %400 == 0 and i%100 != 0: #闰年
        month = month[:j-1]
        if j >2:    #大于2月份
            day = sum(month)+1+k
            
        elif j== 2 and k ==29:    #刚好在闰年闰天
            day = sum(month)+1+k
            
        else:                            
            day = sum(month)+k
    else:    #平年
        month = month[:j-1]
        day = sum(month)+k
        
    print('这一天是这一年的第{}天'.format(day))
    
    半音节

       半音节

      104***[email protected]

    10年前 (2017-06-13)
  11. #0

       guanerye

      lis***[email protected]

    4

    参考方法:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    import calendar
    year = int(input("year: "))
    month = int(input("month: "))
    day = int(input("day: "))
    days = 0
    if 0 < month <= 12:
        _, month_day = calendar.monthrange(year, month)
        if day <= month_day:
            for m in range(1, month):
                _, month_day = calendar.monthrange(year, m)
                days += month_day
            days += day
            print days
        else:
            print "input day error"
    else:
        print "input month error"
    
    guanerye

       guanerye

      lis***[email protected]

    10年前 (2017-06-14)
  12. #0

       Think-dfrent

      iwa***[email protected]

    8

    通过计算输入的日期与相应年份1月1日相差的秒数,然后除以每天的秒数3600*24,即可得到输入日期的天数

    #!/usr/bin/env python3
    
    import time
    
    def datecount(date):
        date0=date[0:4]+"-01-01"
        datet=time.strptime(date,"%Y-%m-%d")     #将输入的字符串转化为时间元组
        date0t=time.strptime(date0,"%Y-%m-%d")  
        dates=time.mktime(datet)                #将时间元组转化为时间戳
        date0s=time.mktime(date0t)
        count=(dates-date0s)/(3600*24)          #输入日期的时间戳减当前年份0101的时间戳除以每天秒数
        return count+1
    
    a=input("请输入日期:格式如2017-06-16\n")
    print("{}是{}年第{}天".format(a,a[0:4],int(datecount(a))))
    Think-dfrent

       Think-dfrent

      iwa***[email protected]

    10年前 (2017-06-16)
  13. #0

       日向翔阳

      wel***[email protected]

    6

    考虑实际的情况,比如输入月份为13月或输入天数为65天时候报错(日期仅校对0-31天,未按照实际日期校对):

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    print('输入年月日以查看某一日期是当年第几天\n')
    year = int(input('请输入年份:\n'))
    month = int(input('请输入月份:\n'))
    day = int(input('请输入日期:\n'))
    months = [31,28,31,30,31,30,31,31,30,31,30,31]
    d = 0
    
    if 0<month<=12:
        if 0<day<=31:
            d = d + day
            if month > 2:
                if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
                    d = d + 1
            for i in range(12):
                if (i+1) < month:
                    d = d + months[i]
                    i = i + 1
            print(d)
        else:
            print('输入的日期有错误')
    else:
        print('输入的月份有错误')
    
    日向翔阳

       日向翔阳

      wel***[email protected]

    10年前 (2017-06-28)
  14. #0

       AKILLII

      616***[email protected]

    4

    通过输入时间点的unix时间戳和输入年份首日的Unix时间戳之间的差,来计算经过的时间

    #coding=utf-8
    import time
    print "Please Enter full number just like 02 01 03"
    y = int(raw_input('Enter the year:'))  #分别输入年月日
    m = int(raw_input('Enter the month:'))
    d = int(raw_input('Enter the day:'))
    a = (y,m,d,00,00,00,00,00,00)  #要求长度为9
    b = (y,01,01,00,00,00,00,00,00)  #输入年份的第一天
    timestampa = time.mktime(a)      #两个都转换为Unix时间戳,即1970.1.1到现在经过的秒数
    timestampb = time.mktime(b)
    timec = int((timestampa - timestampb)/(3600*24))  #输入的时间戳减去年份首天的时间戳等于经过的秒数,再换算成天,取整
    print("There are {} days goes by!".format(timec))
    AKILLII

       AKILLII

      616***[email protected]

    9年前 (2017-07-26)
  15. #0

       小青与

      190***[email protected]

    17

    python3 利用time模块,简洁写法:

    import time
    print(time.strptime('2017-9-20', '%Y-%m-%d')[7])
    小青与

       小青与

      190***[email protected]

    9年前 (2017-09-20)
  16. #0

       阿科.zck

      121***[email protected]

    2

    Python2.x 与 Python3.x 兼容:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    # 觉得自己的逻辑看起来更顺眼,嘻嘻!
    days = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
    yy = int(input('请输入年份:'))
    if yy >= 0:
        mm = int(input('请输入月份:'))
        if 0 < mm < 13:
            dd = int(input('请输入日期:'))
            if ((yy % 4 == 0) and (yy % 100 != 0)) or (yy % 400 == 0):
                if mm <= 2:
                    sum = days[mm - 1] + dd
                else:
                    sum = days[mm - 1] + 1 + dd
            else:
                sum = days[mm - 1] + dd
            print('您输入的时间在这一年的第%d天' % sum)
        else:
            print('您输入的月分不正确')
    else:
        print('请输入正确的公元年')
    阿科.zck

       阿科.zck

      121***[email protected]

    9年前 (2017-11-14)
  17. #0

       Echo

      csz***[email protected]

    4

    分享一下我的答案:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    from functools import reduce
    
    year = int(input('请输入年(如:2017):'))
    month = int(input('请输入月(如:3):'))
    day = int(input('请输入日(如:16):'))
    
    mday = [0,31,28 if year%4 else 29 if year%100 else 28 if year%4 else 29,31,30,31,30,31,31,30,31,30,31]
    
    print('{}年{}月{}日是当年的第{}天'.format(year, month, day, reduce(lambda x,y:x+y, mday[:month])+day))
    Echo

       Echo

      csz***[email protected]

    9年前 (2017-12-05)
  18. #0

       阿土仔

      227***[email protected]

    4

    加入异常处理,确保日期输入格式正确:

    import time
    
    while 1:
        try:
            a=input('请输入日期yyyy-mm-dd:')
            b=time.strptime(a,'%Y-%m-%d') #按输入格式转化成时间格式 local变量
        except ValueError:
            print('请输入正确的日期格式!')
        else:
            b=time.strptime(a,'%Y-%m-%d') #按输入格式转化成时间格式 global变量
            dd=time.strftime('%j',b)  #返回一年中的第几天
            yy=time.strftime('%Y',b)  #返回年份
            print('输入的日期是%s年的第%s天'%(yy,dd))
            break
    阿土仔

       阿土仔

      227***[email protected]

    9年前 (2017-12-05)
  19. #0

       S

      894***[email protected]

    14

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    year = int(input('年:')) 
    mon = int(input('月:')) 
    day = int(input('日:')) 
    Days = [0,31,28,31,30,31,30,31,31,30,31,30,31] 
    
    if (year % 100 != 0 and year % 4 == 0) or (year % 400 == 0):
        Days[2] = 29
    for each in range(0,mon):
        day += Days[each]
    print('天数:%d' %day)
    S

       S

      894***[email protected]

    9年前 (2018-05-21)

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.