Hi Everyone,
I have a question about Practice Mode exercise “Finding The Last Session”.
We want to identify the row corresponding to the last session - in other words, the session with the latest end time (counting both the date and hour).
This is the code I wrote:
import datetime as dt
from csv import reader
opened_file = open("session_log.csv")
read_file = reader(opened_file)
session_log = list(read_file)
session_log_data = session_log[1:]
latest_session_user_id = None
latest_end_time = None
for row in session_log_data:
row = [int(x) for x in row]
end_time = dt.datetime(row[6], row[7], row[8], row[9], row[10])
if latest_end_time == None or latest_end_time < end_time:
latest_end_time = end_time
latest_session_user_id = row[0]
latest_end_str = dt.datetime.strftime(latest_end_time, "%Y/%m/%d %H:%M")
# I found the variable name "latest_start_str" confusing and preferred "latest_end_str"
# I assign "latest_start_str" below to enable auto checking
latest_start_str = latest_end_str
print(latest_session_user_id)
print(latest_end_str)
And these are the results I am getting which are incorrect:
latest_session_user_id: 5483
latest_start_str: 2020/01/02 09:37
I consulted the solution, and other than some variable names, I can see that the if statement is different:
if latest_end == None or latest_end > session_end:
instead of in my code:
if latest_end_time == None or latest_end_time < end_time:
The result (which is marked as correct by answer checking) is:
latest_session_user_id: 244
latest_start_str: 2018/01/01 08:23
I find this confusing as I would have thought that an end date in 2020 is later than an end date in 2018?
Is there an issue with the exercise, or am I not understanding the task correctly?
If anyone has any input, that would be much appreciated.
Thank you.