Scientific Computing with Python Projects - Time Calculator

Tell us what’s happening:

I have issues with evaluating the correct number of days that have passed. When the number of days passed become large, it seems to be one day behind what the actual value is for some reason (e.g. it says 19 days later instead of 20). The code works fine if its only 2 days later for example.

Your code so far

def get_days_later(days):
    if days > 1:
        return f" ({days} days later)"
    elif days == 1:
        return " (next day)"
    else:
        return ""


def add_time(time, add, day=None):
    
    #Getting time data

    time, period = time.split()
    period = period.strip().upper()
    hour, min = time.split(":")
    add_hour, add_min = add.split(":")

    # Variables
    
    week = [
        "monday",
        "tuesday",
        "wednesday",
        "thursday",
        "friday",
        "saturday",
        "sunday"
    ]

    hour, min = int(hour), int(min)
    add_hour, add_min = int(add_hour), int(add_min)

    end_min = min + add_min
    
    if end_min >= 60:
        min = end_min % 60
        hour += 1
    else:
        min = min + add_min

    end_hour = hour + add_hour
    
    cycles = end_hour // 12
    days_passed = end_hour // 24
    hour = end_hour % 12

    # Calculating new time

    if hour == 0:
        hour = 12

    if cycles % 2 != 0:
        if period == "AM":
            period = "PM"
        elif period == "PM":
            period = "AM"

    # Formatting for printing

    if min < 10:
        min = f"0{min}"

    if day:
        days_index = week.index(day.lower())
        final_day = week[(days_index + days_passed) % 7].title()
    
        return f"{hour}:{min} {period}, {final_day}{get_days_later(days_passed)}"
    else:
        return f"{hour}:{min} {period}{get_days_later(days_passed)}"

Challenge: Scientific Computing with Python Projects - Time Calculator

Link to the challenge:

You don’t appear to be handling AM/PM for the original time input.

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