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
#2
Here's the thing, you have two elif month == 14:. The second one is never going to be executed. The first one triggers, and then you have three nested if's. If any of those don't trigger, it skips the other elif month == 14:. You can't change the second elif to an if, because that would mess up the functionality of the final else clause. You need to bring those three nested if's into the elif, and make it all one conditional:

elif month == 14 and (year+1)%4 == 0 and (year+1)%100 == 0 and (year+1)%400 == 0:
That way, if it doesn't trigger, the second elif month == 14: will have a chance to trigger.

Also, I think your 100 year and 400 year checks don't work there. I think you need to reverse them (so the 400 is checked first) and make the 100 a not equals.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you very much!

Now it runs perfectly! ;)
Made three separate "and"-operations instead of the nested if:s. Works like a charm!

elif month == 14 and (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 and (year+1)%100 != 0:
    while day>28 or day<1:
        day = int(input("Day:"))
        if day<1 or day>28:
            print("Out of allowed range 1 - 28")

elif month == 14 and (year+1)%4 == 0 and (year+1)%400 == 0 and (year+1)%100 != 0:
    while day>29 or day<1:
        day = int(input("Day:"))
        if day<1 or day>29:
            print("Out of allowed range 1 - 29")

Went ahead of myself!

This is the correct solution!

elif month == 14 and (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 and (year+1)%4 == 0 and (year+1)%100 != 0:
    while day>29 or day<1:
        day = int(input("Day:"))
        if day<1 or day>29:
            print("Out of allowed range 1 - 28")
Very thankful for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculate AGE in years Kessie1971 17 3,315 Apr-26-2023, 03:36 PM
Last Post: deanhystad
  Tracking leap.py years for gregorian calendar (Exercism org) Drone4four 11 3,658 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 9,846 Mar-26-2018, 04:44 AM
Last Post: PyMan
  human years to dog years jhall710 7 11,356 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