My code wouldn't work

My code def add_time(start, duration):

def add_time(start, duration):
   #1. Get HR & MIN of start and duration and get the AM/PM symbol
    new_time = ''
    nextday = ''
    startV = start.split(':')
    durationV = duration.split(":")
    minutesnvalue = ''
    AMorPM = ''
    for i in range(2):
        minutesnvalue += startV[1][i]
    for i in start:
        if i == "A" or i == "M" or i == "P":
            AMorPM += i

    startV[1] = minutesnvalue
    startHR = startV[0]
    startMIN = startV[1]
    durationHR = durationV[0]
    durationMIN = durationV[1]
    thing = 0
    if int(durationHR) >= 23:
        thing = round(int(durationHR) / 24) 
        newdurHR = ''
        str(durationHR)
        for i in durationHR:
            if i == '.':
                break
            newdurHR += i
        durationHR = newdurHR

        if thing > 1:
            nextday = '(' + thing + ' days later)'
        else:
            nextday = '(next day)'
    #2. Get return value
    hourreturn = int(startHR) + int(durationHR)
    minutereturn = int(startMIN) + int(durationMIN)
    if minutereturn > 59:
        minutereturn -= 60
        hourreturn += 1
    if thing < 1:
        if hourreturn >= 12:
            if AMorPM == "AM":
                AMorPM = "PM"
            elif AMorPM == "PM":
                AMorPM = "AM"
    if hourreturn > 12:
        hourreturn -= 12
    minutereturn = str(minutereturn)
    if len(minutereturn) == 1:
        minutereturn = "0" + minutereturn
    if thing < 1:
        if hourreturn >= 12:
            if AMorPM == "AM":
                AMorPM = "PM"
            elif AMorPM == "PM":
                AMorPM = "AM"
    if hourreturn > 12:
        hourreturn -= 12
    new_time += str(hourreturn)
    new_time += ":"
    new_time += str(minutereturn)
    new_time += " "
    new_time += AMorPM
    if thing > 0:
        new_time += ' '
        new_time += nextday
    return new_time

does return 2:59 AM (next day) when add_time('2:59 AM', '24:00') is called in print(), however, 3. Expected time to end with '(next day)' when it is the next day is still failed. Can you please help me? Thank you :smiley:
(Edit: I spotted a potential problem and then fixed it, but it still doesn’t work. The new code is here with a small tweak):


def add_time(start, duration):
    #1. Get HR & MIN of start and duration and get the AM/PM symbol
    new_time = ''
    nextday = ''
    startV = start.split(':')
    durationV = duration.split(":")
    minutesnvalue = ''
    AMorPM = ''
    for i in range(2):
        minutesnvalue += startV[1][i]
    for i in start:
        if i == "A" or i == "M" or i == "P":
            AMorPM += i

    startV[1] = minutesnvalue
    startHR = startV[0]
    startMIN = startV[1]
    durationHR = durationV[0]
    durationMIN = durationV[1]
    thing = 0
    if int(durationHR) > 23:
        thing = round(int(durationHR) / 24) 
        newdurHR = ''
        str(durationHR)
        for i in durationHR:
            if i == '.':
                break
            newdurHR += i
        durationHR = newdurHR

        if thing > 1:
            nextday = '(' + thing + ' days later)'
        else:
            nextday = '(next day)'
    #2. Get return value
    hourreturn = int(startHR) + int(durationHR)
    minutereturn = int(startMIN) + int(durationMIN)
    if minutereturn > 59:
        minutereturn -= 60
        hourreturn += 1
    if thing < 1:
        if hourreturn >= 12:
            if AMorPM == "AM":
                AMorPM = "PM"
            elif AMorPM == "PM":
                AMorPM = "AM"
    if hourreturn > 12:
        hourreturn -= 12
    minutereturn = str(minutereturn)
    if len(minutereturn) == 1:
        minutereturn = "0" + minutereturn
    if thing < 1:
        if hourreturn >= 12:
            if AMorPM == "AM":
                AMorPM = "PM"
            elif AMorPM == "PM":
                AMorPM = "AM"
    if hourreturn > 12:
        hourreturn -= 12
    new_time += str(hourreturn)
    new_time += ":"
    new_time += str(minutereturn)
    new_time += " "
    new_time += AMorPM
    if thing > 0:
        new_time += ' '
        new_time += nextday
    return new_time

Can you talk about what thing is and how this makes sure you always add (next day)? It seems to me that you can go to the next day with a duration that is less than 23 hours.

Welcome to the forum :wave:

Can you please provide a link to the exercise, instructions etc?

Theres an if code that checks if the time passed(durationHR) is greater than 23 (I editted my code look at the original post again), then the code inside runs, dividing int(durationHR) by 24 and rounding it all together and assigning it with the variable named thing. Then there’s a variable called newdurHR for later use and durationHR is turned into a string (I editted my code and I’m going to edit the post). Then I round durationHR with the for i in durationHR code and then check if the amount of days passed (24 hours) is greater than 1. If yes, nextday is ‘((amount of days) days later)’. If no, nextday is ‘(next day)’

So you have exactly the problem I pointed out. You can end up on the next day with an event that has fewer than 23 hours of duration