Scientific Computing with Python Projects - Time Calculator

Tell us what’s happening:

Hello, first of all, thank you very much for your time. My problem is that my program can print the result, but it doesn’t return anything. I would really like to identify the error so as not to make it again.

Your code so far

def day_of_the_week(day, n_days):
    week = ['Sunday', 'Monday','Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
    days_name = ""
    current_d = week.index(day.capitalize())
    reset_week = week[current_d:] + week[:current_d]
    
    if n_days > 7:
        fix_d = n_days % 7
        days_name = f', {reset_week[fix_d]}'       
        
    else:
        fix_d = n_days
        days_name = f', {reset_week[fix_d]}' 

    return days_name
def add_time(start, duration, day = None):
    
    start_d1 = start.split(':')
    duration_d = duration.split(':')
    start_d2 = start_d1[1].split()
    hours = int(start_d1[0]) + int(duration_d[0])
    minutes = int(start_d2[0]) + int(duration_d[1])
    am_pm = start_d2[1]
    days = 0
    days_note = ""
    new_time = ""
    
    if hours > 24:
        days = int(hours / 24)
        hours = hours % 24 

    if minutes >= 60:
        hours += 1
        minutes -= 60
        
    if hours > 12:
        hours = hours - 12 
        if am_pm == "PM": 
            days += 1
            am_pm = 'AM'
        else:
            am_pm = 'PM'

    elif hours == 12:
        if am_pm == "PM": 
            days += 1
            am_pm = 'AM'
        else:
            am_pm = 'PM'    

    if day:
        day_n = day_of_the_week(day, days)

    if days > 0:
        if days == 1:
            days_note = " (next day)"
        else:
            days_note = f" ({days} days later)" 
    
    new_time = str(hours) + ':' + str(minutes) + am_pm + day_n + days_note

    return new_time

    
(add_time('3:00 PM', '60:10', 'Wednesday'))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

Scientific Computing with Python Projects - Time Calculator

what have you used to check that it prints the value?

It does return new_time

What error are you getting, exactly?

Thank you, you were right, it’s return new_time. I only needed to add one case to give the right format to the minutes that were less than 10.

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