-2

Nooby Coder here! My assignment is to code something that prints the day of the week of any given date. My code is working fine but when I run anything that isn't in the correct format (i.e. "//2011" or "12/a/2011") it will give me this error:

line 55, in main if is_date_valid(month, day, year) == False:
UnboundLocalError: local variable 'month' referenced before assignment

Although it does run fine if I try "13/2/2011". Please help me find my problem, as my teacher didn't know the solution when I asked him about it! Here is the whole code if necessary (ignore my comments :p) Thank you very much :D

import sys
DAYS_IN_MONTH = ('31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31')
MONTHS_IN_WORDS = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
DAYS_OF_WEEK = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')

def get_date():
    str_date = input('Enter date: ')#.strip() = method :D
    parts = str_date.split('/')#pasts = ['12', '2', '2011']
    length = len(parts)
    if length != 3:
        raise ValueError('Invalid format for date.')
    for i in range(length):
        parts[i] = parts[i].strip()
        if len(parts[i]) == 0 or not(parts[i].isdigit()):
            raise ValueError('Invalid format for date.')
    return(int(parts[0]), int(parts[1]), int(parts[2]))

def is_date_valid(month, day, year): #is_date_valid = name of function
    if month < 1 or month > 12 or day < 1 or year < 0:
        return False
    if month != 2:
        return int(day) <= int(DAYS_IN_MONTH[month-1])
    additional = 0
    if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
        additional = 1
    return int(day) <= int(DAYS_IN_MONTH[1]) + int(additional)

#month, day, year = arguments of function

def date_as_string(month, day, year):
    if month > 0 and month < 13:
        return MONTHS_IN_WORDS[month-1] + ' ' + str(day) + ', ' + str(year)


def day_of_week(month, day, year):
    if month < 3:
        month += 12
        year -= 1
    m = month
    q = day
    J = year // 100
    K = year % 100
    h = (q + 26*(m+1)//10 + K + K//4 + J//4 - 2*J) % 7
    dayweek = DAYS_OF_WEEK[h-2]
    return dayweek

def main():
    print('This program calculates the day of the week for any date.')
    try:
        (month, day, year) = get_date()
    except ValueError as error:
        print("ERROR:", error)
        sys.exit(1)
    if is_date_valid(month, day, year) == False:
        print(str(month) + '/' + str(day) + '/' + str(year) + ' is an invalid date.')
        print()

    else: 
        print(date_as_string(month, day, year) + ' is a ' + day_of_week(month, day, year) + '.')
        print()



#any function call that can raise an error must be put inside a try block
if __name__ == '__main__':
    main()

1 Answers1

0

Firstly, you enter a date with no month, your program will tell you that you don't have a month. This doesn't seem to be much of a problem?

Secondly, and both your examples will in fact give a "ERROR: Invalid format for date.", and not the exception you claim. Your last example. 13/2/2011, will give "13/2/2011 is an invalid date.", but if you change it to a valid date, like "12/2/2011" it will give "February 13, 2011 is a Sunday."

Hence your program is working perfectly.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251