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

Python 基础教程
(追記) (追記ここまで)

Python 练习实例4

Python 100例 Python 100例

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

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

程序源代码:

实例

#!/usr/bin/python3year = 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)if0 < month <= 12: sum = months[month - 1]else: print('data error')sum += dayleap = 0if(year % 400 == 0)or((year % 4 == 0)and(year % 100 != 0)): leap = 1if(leap == 1)and(month > 2): sum += 1print('it is the %dth day.' % sum)

以上实例输出结果为:

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

Python 100例 Python 100例

AI 思考中...

19 篇笔记 写笔记

  1. #0
    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
    
    9年前 (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

    646***[email protected]

    9年前 (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]

    9年前 (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

    tum***@126.com

    9年前 (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]

    9年前 (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

    511***[email protected]

    9年前 (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

    412***[email protected]

    9年前 (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]

    9年前 (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

    123***3.123

    9年前 (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]

    9年前 (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

    lis***[email protected]

    9年前 (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

    iwa***[email protected]

    9年前 (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]

    9年前 (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

    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

    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

    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
    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)
    8年前 (2018年05月21日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /