The goal of the program is to:
- validate inputs formats, and the date range should not be more than 2 months
- No input/invalid input -> return the first or second half of the current month based on the day of the month
- For the first half is from day 1 to day 15 for the second half is from day 16 till the end of the month
The hardcode, which will be the first code snippet, works as expected with the correct output, however the date validation in the second code snippet does not jump to the method indicated in the except block, which is causing me problems and not displaying correct output.
import os, sys
import datetime as dt
if __name__ == '__main__':
validate_range('2003/12/23', '2003/12/22')
validate_range('2003/10/23', '2003/11/22')
validate_range('2003/12/23', '2003/10/22')
validate_range('2003/7/23', '2003/10/22')
Code snippet with correct output (does not check if dates are correct format):
def validate_range(first_date, second_date):
start_date = dt.datetime.strptime(first_date, '%Y/%m/%d') # Get first date
end_date = dt.datetime.strptime(second_date, '%Y/%m/%d') # Get second date
num_months = (end_date.year - start_date.year) * 12 (
end_date.month - start_date.month) # Get year/month within 2 months
if num_months >= 0 and num_months <= 2:
print("In Range")
else:
current_day = dt.datetime.today().day
if current_day >= 1 and current_day <= 15:
print("First half")
if current_day >= 16 and current_day <= 32:
print("Second half")
Correct Output:
In Range
In Range
First half
First half
Code snippet that does not display correct output (with date validation):
def read_args(first_date, second_date):
try:
start_date = dt.datetime.strptime(first_date, '%Y/%m/%d') # Get first date
end_date = dt.datetime.strptime(second_date, '%Y/%m/%d') # Get second date
v_range(start_date, end_date)
except:
w_range()
def v_range(first_date, second_date):
num_months = (second_date.year - first_date.year) * 12 (
second_date.month - first_date.month) # Get year/month within 2 months
if num_months >= 0 and num_months <= 2:
print("In Range")
def w_range():
current_day = dt.datetime.today().day
if current_day >= 1 and current_day <= 15:
print("First half")
if current_day >= 16 and current_day <= 32:
print("Second half")
Output:
In Range
In Range
CodePudding user response:
Keeping with your use of try / except, might need some improvement, but this example demonstrates it is possible:
import os, sys
import datetime as dt
def read_args(first_date, second_date):
try:
start_date = dt.datetime.strptime(first_date, '%Y/%m/%d') # Get first date
end_date = dt.datetime.strptime(second_date, '%Y/%m/%d') # Get second date
result = v_range(start_date, end_date)
except:
pass
# w_range()
def v_range(first_date, second_date):
num_months = (second_date.year - first_date.year) * 12 (
second_date.month - first_date.month) # Get year/month within 2 months
if num_months >= 0 and num_months <= 2:
print("In Range")
else:
w_range()
def w_range():
current_day = dt.datetime.today().day
if current_day >= 1 and current_day <= 15:
print("First half")
if current_day >= 16 and current_day <= 32:
print("Second half")
if __name__ == '__main__':
read_args('2003/12/23', '2003/12/22')
read_args('2003/10/23', '2003/11/22')
read_args('2003/12/23', '2003/10/22')
read_args('2003/7/23', '2003/10/22')
