Scientific Computing with Python Projects - Time Calculator

Tell us what’s happening:
Why am I receiving this error

 python main.py
01:08 AM (0 days later)
Traceback (most recent call last):
File “/home/runner/boilerplate-time-calculator/time_calculator.py”, line 15, in add_time
duration_time = datetime.strptime(duration, ‘%H:%M’)
File “/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/_strptime.py”, line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File “/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/_strptime.py”, line 349, in _strptime
raise ValueError(“time data %r does not match format %r” %
ValueError: time data ‘466:02’ does not match format ‘%H:%M’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “main.py”, line 7, in
print(add_time(“8:16 PM”, “466:02”))
File “/home/runner/boilerplate-time-calculator/time_calculator.py”, line 17, in add_time
raise ValueError(“Incorrect duration format, should be in ‘HH:MM’”)
ValueError: Incorrect duration format, should be in ‘HH:MM’
exit status 1

.

**import datetime
from datetime import timedelta , datetime

def add_time(start, duration, day_of_week=False):
days_of_the_week_index = {“monday”: 0, “tuesday”: 1, “wednesday”: 2, “thursday”: 3, “friday”: 4, “saturday”: 5, “sunday”: 6}
days_of_the_week_array = [“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”]
pattern = ‘%I:%M %p’

try:
    start_time = datetime.strptime(start, pattern)
except ValueError:
    raise ValueError("Incorrect start time format, should be in 'HH:MM AM/PM'")

try:
    duration_time = datetime.strptime(duration, '%H:%M')
except ValueError:
    raise ValueError("Incorrect duration format, should be in 'HH:MM'")

end_time = start_time + timedelta(hours=duration_time.hour, minutes=duration_time.minute)

end_time_str = end_time.strftime("%I:%M %p")

if day_of_week:
    day_of_week = day_of_week.lower()
    if day_of_week not in days_of_the_week_index:
        raise ValueError("Incorrect day of the week, should be one of ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']")
    index = int((days_of_the_week_index[day_of_week]) + (end_time.day - start_time.day)) % 7
    new_day = days_of_the_week_array[index]
    end_time_str += ", " + new_day

if start_time.day != end_time.day:
    days_diff = (end_time - start_time).days
    end_time_str += " (next day)" if days_diff == 1 else f" ({days_diff} days later)"

return end_time_str

**

Your browser information:

Chrome
Version 111.0.5563.65 (Official Build) (64-bit)

Challenge: Scientific Computing with Python Projects - Time Calculator

Link to the challenge:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.