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
asked Mar 24, 2014 at 2:52
user3251511
1872 gold badges4 silver badges16 bronze badges
2 Answers 2
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)
answered Mar 24, 2014 at 3:04
Jeffrey Tang
7936 silver badges17 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
user3251511
Thanks! If I want to implement try and except in my last code, should try and except be placed at the middle or at the end? Is the indentation the same line following "def"?
Jeffrey Tang
Since both your validate functions return True and False, you just need to check their return values. Try and except are still unneeded in your example. Your third if clause in count_days should be checking for the return value of less_than_equal, not something it printed. Did I understand correctly?
user3251511
But what about if the function meets with some errors and I want to continue the loop till the end?
Jeffrey Tang
Ah, I see what you mean.
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
answered Mar 24, 2014 at 4:09
Hugh Bothwell
57k9 gold badges91 silver badges103 bronze badges
Comments
lang-py
return Falsestatement in yourexceptblocktry/exceptblocks are used for and if/how it applies here. The way you're using it is not how it's meant to be used. It would be pointless to explain how to make your code work using it. Just do it right.