user3251511
user3251511

Reputation: 187

Try and Except in Python

My code is:

def less_than_equal(start_day, start_mon, start_year, \
                end_day, end_mon, end_year):
    try:
        start_day <= end_day and start_mon <= end_mon
        return True
    except:
        start_day > end_day or start_mon > end_mon or start_year >end_year
        print("Start date must be less than or equal end date.")

where

>>> less_than_equal(12, 4, 1111, 12, 5, 1111)
True
>>> less_than_equal(12, 7, 1111, 12, 5, 1111)
"Start date must be less than or equal end date."  (but my code gives True)
>>> less_than_equal(17, 7, 1111, 12, 5, 1111)
"Start date must be less than or equal end date." (mine gives True here too)

I also am very confused on how to use raise and try and except. Which is placed within the body of the code, which is placed at the very end? For example, under the #TODO

def count_days(start_date, end_date):
    # date is represented as a string in format dd/mm/yyyy
    start_day, start_mon, start_year = get_day_month_year(start_date)
    end_day, end_mon, end_year = get_day_month_year(end_date)

# TODO: check for data validity here #
# if start date is not valid...
# if end date is not valid...
# if start date > end date...
    if is_valid( start_day, start_mon, start_year)  == False:
        raise ValueError( "Not a valid date: " + start_date )
    elif is_valid(end_day, end_mon, end_year) == False:
        raise ValueError(  "Not a valid date: " + end_date )
    if less_than_equal(start_day, start_mon, start_year, \
         end_day, end_mon, end_year) == "Start date must be less than or equal end date.":
        raise ValueError( "Not a valid date: " + start_date )

# lazy - let the computer count from start date to end date
   count = 0
    while less_than_equal(start_day, start_mon, start_year, end_day, end_mon, end_year):
        count = count + 1
        start_day, start_mon, start_year = next_date(start_day, start_mon, start_year)

# exclude end date
    return count - 1

Thanks a lot

Upvotes: 1

Views: 1016

Answers (2)

Jeffrey Tang
Jeffrey Tang

Reputation: 793

You don't need to use try and except here.

def less_than_equal(start_day, start_mon, start_year, \
            end_day, end_mon, end_year):
    if start_day <= end_day and start_mon <= end_mon:
        return True
    elif start_day > end_day or start_mon > end_mon or start_year >end_year:
        print("Start date must be less than or equal end date.")
        return False

Try and except are used for catching exceptions.

For your second question, you can

count = 0
while True:
    try:
        if not less_than_equal(start_day, start_mon, start_year, end_day, end_mon, end_year):
            break
    except TypeError: # for example
        break

    count = count + 1
    start_day, start_mon, start_year = next_date(start_day, start_mon, start_year)

Upvotes: 5

Hugh Bothwell
Hugh Bothwell

Reputation: 56624

There are built-in library functions which would make this much simpler:

import datetime
import time

def parse_date(datestring):
    dt = time.strptime(datestring, "%Y/%m/%d")
    return datetime.date(dt.tm_year, dt.tm_mon, dt.tm_mday)

def count_days(start_date, end_date):
    s = parse_date(start_date)
    e = parse_date(end_date)

    if s > e:
        raise ValueError("Start date must be <= end date")
    else:
        return (e - s).days

then

count_days("2012/2/27", "2012/3/5")   # => 7

Upvotes: 1

Related Questions