Home > other >  In my code, why doesn't my list sort from the earliest to the latest in dates? Can someone plea
In my code, why doesn't my list sort from the earliest to the latest in dates? Can someone plea

Time:07-29

from datetime import date, timedelta, time, datetime

# 1 Complete read_date()
def read_date(date_object):
    """Read a string representing a date in the format 2121-04-12, create a
    date object from the input string, and return the date object
    """
    dt_string = '2121-04-12'
    date_object = datetime.strptime(date_object, '%Y-%m-%d').date()
    return date_object
# 2. Use read_date() to read four (unique) date objects, putting the date objects in a list
date1 = input()
date2 = input()
date3 = input()
date4 = input()

date1_read = read_date(date1)
date2_read = read_date(date2)
date3_read = read_date(date3)
date4_read = read_date(date4)

list_date = []
list_date.append([date1, date2, date3, date4])
split_list = 

# 3. Use sorted() to sort the dates, earliest first
list_sorted = sorted(list_date)
print(list_sorted)
# 4. Output the sorted_dates in order, earliest first, in the format mm/dd/yy
new_format = 
# 5. Output the number of days between the last two dates in the sorted list
#    as a positive number

# 6. Output the date that is 3 weeks from the most recent date in the list

# 7. Output the full name of the day of the week of the earliest day

Under #3, I need to sort the dates from earliest in a specific format. I need help sorting the list as my code does not sort it. I also need help in formatting the entire list in a specific way.

CodePudding user response:

The dates are not sorted because you are assigning the datetime object to the variables date?_read, which are never added to the list_date before applying the sorted() built-in function. That means the list elements are sorted as strings, not as dates. Here are the steps you asked for, that are kind of self-explanatory, I'd like though to have more datails about point 6, which doesn't tell much, 3 weeks in the past? In the future?

from datetime import date, timedelta, time, datetime
import sys

# 1 Complete read_date()
def read_date(date_object):
    """Read a string representing a date in the format 2121-04-12, create a
    date object from the input string, and return the date object
    """
    date_object = datetime.strptime(date_object, '%Y-%m-%d').date()
    return date_object

# 2. Use read_date() to read four (unique) date objects, putting the date objects in a list

list_date = []
n_dates = 4

# Here you could check with an infinite loop the inputs
# Not being the main request, I opted for a more straightforward version
try: 
    for _ in range(n_dates):
        list_date.append(read_date(input()))
except ValueError:
    print('Invalid format')
    sys.exit(1) 

# 3. Use sorted() to sort the dates, earliest first
list_sorted = sorted(list_date)
print(list_sorted)

# 4. Output the sorted_dates in order, earliest first, in the format mm/dd/yy
for date in list_sorted:
    print(date.strftime('%m/%d/%y'))
# 5. Output the number of days between the last two dates in the sorted list
#    as a positive number
print(abs((list_sorted[-1] - list_sorted[-2]).days))

# 6. Output the date that is 3 weeks from the most recent date in the list
# 3 weeks ??
 
# 7. Output the full name of the day of the week of the earliest day
print(list_sorted[0].strftime('%A'))
  • Related