def yer1(year,month,day):
# tmonth = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
# 判斷是否閏年
if year % 4 == 0:
# 判斷月份的天數
if month == 2 and (day > 29 or day < 1):
print("日期錯誤%s,只有 29天,請重新輸入" % month)
return false
tday = (0,31,29,31,30,31,30,31,31,30,31,30,31)
else:
if month == 2 and (day > 28 or day < 1):
print("日期錯誤%s,只有 28天,請重新輸入" % month)
return false
tday = (0,31,28,31,30,31,30,31,31,30,31,30,31)
days = 0
for i in range(0,month):
days = days + tday[i]
days = days + day
print("%s 年 %s 月 %s日 是當年的第 %s天"%(year,month,day,days))
def open():
year = int(input("請輸入年份:"))
month1 = int(input("請輸入月份:"))
day1 = int(input("請輸入日期:"))
# 判斷月份是否正確
if month1 >12 or month1 <1:
print("月份錯誤,請重新輸入")
return
tuple1 = (1,3,5,7,8,10,12)
if month1 in tuple1 and ( day1>31 or day1 <1):
print("日期錯誤%,只有 31天,請重新輸入"%month1)
return
tuple2 = (4, 6, 9, 11,)
if month1 in tuple2 and (day1 > 30 or day1 < 1):
print("日期錯誤%,只有 30天,請重新輸入" % month1)
return
yer1(year,month1,day1)
def main():
while true:
open()
if __name__=="__main__":
main()
輸入某年某月某日,判斷這一天是這一年的第幾天?
案例 輸入某年某月某日,判斷這一天是這一年的第幾天?方法一 system.out.println 輸入某年某月某日 scanner sc new scanner system.in system.out.println 年 int year sc.nextint system.out.println...
輸入某年某月某日,判斷這一天是這一年的第幾天?
解題思路 因為平年和閏年的2月時間不一樣,所以要先判斷是平年還是閏年。接著根據switch語句,把所輸入的月份分開算,前面的月份所經歷的時間,加上本月的第一幾天日期。因為從2月後開始,每個月的計算都要判斷一下平年還是閏年,所以把這個寫成了乙個函式。這樣寫 還是有點兒多,也可以一開始就判斷是平年還是閏...
輸入某年某月某日,判斷這一天是這一年的第幾天
分析方式 以具體的某個月份為例,例如2019年3月5號,先把前兩個月的天數加起來,再加5,特殊情況的是遇到閏年,大於2月的在天數後加1 list 1 0,31,59,90,120,151,181,212,243,273,304,334 float day 0 year int input year ...