Python Forum
Problem with code to calculate weekday for leap years!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with code to calculate weekday for leap years!
#1
Hi!

i have a school assignment in which i just cant figure out how to narrow the inputs to only calculate with allowed dates which only occur on leap years!
The program functions fine for all other dates, except for when the user enters values which i significant for leap years.
I have noticed that there it something with just that string that makes the whole program to skip the lines where otherwise the user are supposed to enter a value for day. I just cant figure out what the error might be. Please help!

How can I make the program to follow specifik instructions only on the month on january on leap years (value 2 or 14 in the code) which only allows the user to enter integers between 1 and 29?

This is the closest i have come:

elif month == 14:
    if (year+1)%4 == 0:
        if (year+1)%100 == 0:
            if (year+1)%400 == 0:
                while day>29 or day<1:
                    day = int(input("Day:"))
                    if day<1 or day>29:
                        print("Out of allowed range 1 - 29")
The following is the program in full:

year = 0
while year>9999 or year<1583:
    year = int(input("Year:"))
    if year<1583 or year>9999:
        print("Out of allowed range 1583 - 9999")

month = 0
while month>12 or month<1:
    month = int(input("Month:"))
    if month<1 or month>12:
        print("Out of allowed range 1 - 12")
if month == 1 or month == 2:
    month += 12
    year -= 1

day = 0
if month == 4 or month == 6 or month == 9 or month == 11:
    while day>30 or day<1:
        day = int(input("Day:"))
        if day<1 or day>30:
            print("Out of allowed range 1 - 30")

# Somewhere in the following lines the error occur!
elif month == 14:
    if (year+1)%4 == 0:
        if (year+1)%100 == 0:
            if (year+1)%400 == 0:
                while day>29 or day<1:
                    day = int(input("Day:"))
                    if day<1 or day>29:
                        print("Out of allowed range 1 - 29")

elif month == 14:
    while day>28 or day<1:
        day = int(input("Day:"))
        if day<1 or day>28:
            print("Out of allowed range 1 - 28")

else:
    while day>31 or day<1:
        day = int(input("Day:"))
        if day<1 or day>31:
            print("Out of allowed range 1 - 31")

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")
else:
    print ("Something went wrong")

Sorry for posting code incorrectly in previous post!

year = 0
while year>9999 or year<1583:
    year = int(input("Year:"))
    if year<1583 or year>9999:
        print("Out of allowed range 1583 - 9999")

month = 0
while month>12 or month<1:
    month = int(input("Month:"))
    if month<1 or month>12:
        print("Out of allowed range 1 - 12")
if month == 1 or month == 2:
    month += 12
    year -= 1

day = 0
if month == 4 or month == 6 or month == 9 or month == 11:
    while day>30 or day<1:
        day = int(input("Day:"))
        if day<1 or day>30:
            print("Out of allowed range 1 - 30")

# Somewhere in the following lines the error occur!
elif month == 14:
    if (year+1)%4 == 0:
        if (year+1)%100 == 0:
            if (year+1)%400 == 0:
                while day>29 or day<1:
                    day = int(input("Day:"))
                    if day<1 or day>29:
                        print("Out of allowed range 1 - 29")

elif month == 14:
    while day>28 or day<1:
        day = int(input("Day:"))
        if day<1 or day>28:
            print("Out of allowed range 1 - 28")

else:
    while day>31 or day<1:
        day = int(input("Day:"))
        if day<1 or day>31:
            print("Out of allowed range 1 - 31")

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")
else:
    print ("Something went wrong")
Reply


Messages In This Thread
Problem with code to calculate weekday for leap years! - by Event - Dec-15-2018, 03:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Frog codility leap sort variant MoreMoney 5 435 Apr-06-2024, 08:47 PM
Last Post: deanhystad
  Calculate AGE in years Kessie1971 17 3,533 Apr-26-2023, 03:36 PM
Last Post: deanhystad
  Tracking leap.py years for gregorian calendar (Exercism org) Drone4four 11 3,810 Oct-14-2022, 03:20 PM
Last Post: betinajessen
  python age calculator need to find the number of years before they turn 100 not using orangevalley 4 10,001 Mar-26-2018, 04:44 AM
Last Post: PyMan
  human years to dog years jhall710 7 11,459 May-08-2017, 05:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020