Scientific Computing with Python Projects - Time Calculator

I was coding on pycharm as my replit was not working, and after writing the whole code which was running fine on pycharm and visual studio code and other compilers but on replit it is showing that the index is out of range which is clearly not true otherwise the other compilers would have given the same error as well.

def add_time(start, duration,*args):
    """(string,string,string (optional))-> string

    This function takes in arguments of time in 12 hour format and duration and then
    adds the duration to time and tells the new time and day

    pre-condtion: the number of minutes are between 0 and 59

    >>>add_time("3:00 PM", "3:10")
    6:10 PM

    >>>add_time("11:30 AM", "2:32", "Monday")
    2:02 PM, Monday

    >>>add_time("11:43 PM", "24:20", "tueSday")
    12:03 AM, Thursday (2 days later)
    """
    new_time=''
    time=''
    dur=''
    count=0
    zone=''
    day=None
    if len(args)!=0:
        day=args[0]

    time=start.split()
    dur=duration.split(':')
    hours_start=time[0].split(':')

    if time[1]=="PM":
        hours_str=int(hours_start[0])
        hours_str+=12
        hours_start[0]=str(hours_str)

    hours=int(hours_start[0])+int(dur[0])
    minutes=int(hours_start[1])+int(dur[1])
    new_time=str(hours)+':'+str(minutes)

    if minutes>59: # to check if the minutes are greater than 59
        hours+=1
        minutes-=60
    if minutes<10:
        mins="0"+str(minutes)
        minutes=mins

    while hours>=24: # to check if the hours are greater than 24
        count+=1
        hours-=24

    if hours>12: # to check if PM or AM
        zone='PM'
        hours-=12
    elif hours==12:
        zone="PM"
    elif hours==0:
        hours+=12
        zone='AM'
    else:
        zone="AM"

    days=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    Day=day
    count2=0

    for i in days:
        if day==i:
            break
        else:
            count2+=1

    j = count2
    for i in range(count2,count+count2):
        if (i+1)%7==0:
            j=0
            Day=days[j]
        else:
            Day=days[j+1]
            j += 1

    if count!=0:
        if count==1:
            if day!=None:
                new_time = str(hours) + ":" + str(minutes) + " " + zone +", "+Day+ " (next day)"
            else:
                new_time = str(hours) + ":" + str(minutes) + " " + zone+" (next day)"
        elif count>1:
            if day!=None:
                new_time = str(hours) + ":" + str(minutes) + " " + zone +", "+Day+ " (" + str(count) + " days later)"
            else:
                new_time = str(hours) + ":" + str(minutes) + " " + zone+" ("+str(count)+" days later)"
    else:
        new_time = str(hours)+":"+str(minutes)+" "+zone

    return new_time

print(add_time("8:16 PM", "466:02", "Monday"))

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Time Calculator

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

What happens if you run your code locally or on pycharm with the following?

add_time("11:06 PM", "2:02")

I get IndexError: list index out of range with your code.

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