Home > Software design >  when i run it through a VPL it passes 13/16 tests
when i run it through a VPL it passes 13/16 tests

Time:09-22

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)


month=[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:
        month =12
        year-=1        
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: "))
    

weekday = ( day   13*(month 1)// 5   year   year// 4- year//100 year// 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.")`

This is the code I wrote and when i run it through a VPL it passes 13/16 tests. The ones it does not pass is :

year: 1900 month:2 day: 29
year: 2018 month:1 day: 32
year: 2018 month:2 day: 32

I've been trying different things but I dont know what is wrong ie. how to correct it. Excuse the low skill level, I've been learning for a week only.

CodePudding user response:

First problem is this code:

if month==1 or month==2:
    month =12
    year-=1

If month is 1 or 2 then month becomes 13 or 14, then the next validation tests fail to detect the bad day of the month.

And next problem, this fails to check for condition day < 1 or day > 28 if leap_year = False as in case of 1900, month=2, day=29.

if month==2:
    if leap_year:
        while day<1 or day>29:
               ...

Can rewrite like this:

if month==2:
    if leap_year:
        max_day = 29
    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: "))
  • Related