日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 实例四

發布時間:2023/12/20 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 实例四 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目:輸入某年某月某日,判斷這一天是這一年的第幾天?

程序分析:

月份天數:

月份天數
2平年28天,閏年29天
1,3,5,7,8,10,1231
4,6,9,1130

?

?閏年:

1、非整百年:能被4整除的為閏年。(如2004年就是閏年,2100年不是閏年) ? 2、整百年:能被400整除的是閏年。(如2000年是閏年,1900年不是閏年) >>> L=[31,31,30,31,30,31,31,30,31,30,31] >>> def caldate(a,b,c):s=0if(a%100!=0 and a%4==0 or a%100==0 and a%400==0):L.insert(1,29)else:L.insert(1,28)for i in range(b-1):s=s+L[i]return s+c>>> caldate(2016,1,2) 2 >>> caldate(2016,3,2) 62

改進版:考慮了月份和天數的有效性(哈哈,對比網上的答案,一看自己的代碼就像是菜鳥級的,還有很多需要學習的地方)

def caldate(a,b,c):L=[31,31,30,31,30,31,31,30,31,30,31]s=0Leap=0if(a%100!=0 and a%4==0 or a%100==0 and a%400==0):L.insert(1,29)Leap=1else:L.insert(1,28)if 0<b<=12:if 0<c<=31:if((b==2)and(Leap==1)and(c<=29)or((b==2)and(Leap==0)and(c<=28))):for i in range(b-1):s=s+L[i]return s+celse:print 'The February should not greater than 28 or 29'else:print 'The date is error'else:print 'The month is invalid'

輸出:

>>> caldate(2016,4,33) The date is error >>> caldate(2017,2,30) The February should not greater than 28 or 29 >>> caldate(2017,2,28) 59 >>> caldate(2017,2,29) The February should not greater than 28 or 29 >>> caldate(2017,13,29) The month is invalid

網上答案:

#!/usr/bin/python # -*- coding: UTF-8 -*-year = int(raw_input('year:\n')) month = int(raw_input('month:\n')) day = int(raw_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.

?

轉載于:https://www.cnblogs.com/evablogs/p/6754981.html

總結

以上是生活随笔為你收集整理的python 实例四的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。