year=int(input("year: "))
while year<1583 or year>9999。
print("Out of allowed range 1583 to 9999")
year=int(input("year: "))
leap_year=year%4==0 and (year%100 ! =0 or year%400==0)
月=[1,12]
month=int(input("month: "))
while month<1 or month >12:
print ("Out of allowed range 1 to 12")
month=int(input("month: ")
day=int(input("Day: ")
if month==1 or month== 2:
月 ==12:
if month in [1,3, 5,7,8,10,12】。]
while day<1 or day> 31:
print("Out of allowed range 1 to 31")
day=int(input("Day: ")
if month in [4,6,9,11] 。
while day<1 or day >30:
print("Out of allowed range 1 to 30")
day=int(input("Day: ")
if month==2:
if leap_year:
while day<1 or day>29:
print("Out of allowed range 1 to 29")
day=int(input("Day: ")
星期天 = ( 日 13*(月 1)// 5 年 年// 4-年//100 年//400)% 7
if weekday==0:
print("It is a Saturday." )
elif weekday==1:
print("It is a Sunday." )
elif weekday==2:
print("It is a Monday." )
elif weekday==3:
print("It is a Tuesday." )
elif weekday==4:
print("It is a Wednesday." )
elif weekday==5:
print("It is a Thursday." )
elif weekday==6:
print("It is a Friday.")`。
這是我寫的代碼,當我通過VPL運行它時,它通過了13/16個測驗。它沒有通過的測驗是:
年。1900 month:2 day: 29
年:2日:29 2018月:1日。32
年。2018月:2日。32
我一直在嘗試不同的東西,但我不知道哪里出了問題,即如何糾正它。請原諒我的技術水平低,我只學了一個星期。
uj5u.com熱心網友回復:
第一個問題是這個代碼:
if month==1 or month==2:
月 ==12:
year-=1: year =12.
如果month是1或2,那么month就會變成13或14,那么接下來的驗證測驗就無法檢測到這個月的壞日子。
而下一個問題是,如果閏年=False,如1900年,月=2,日=29,這就無法檢查出條件日< 1或日> 28。
if month==2:
if leap_year:
while day<1 or day>29:
...
可以這樣改寫:
if month== 2:
if leap_year:
max_day = 29: if leap_year.
else:
max_day = 28
while day < 1 or day > max_day:
print(f "Out of allowed range 1 to {max_day}")
day=int(input("Day: ")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/331572.html
標籤:
