Time Calculator (Help! Make code cleaner)

Tell us what’s happening:
My code already works, but I want to make it shorter. Is there another way of doing it since I can’t do the part of the days of the week without many if statements. I know that part is optional but still I want to try it.

Basically I want to make it cleaner but I don’t know how.
Thank you so much

Your code so far

def add_time(start, duration):
    i = 0
    count = 0
    time, clock = start.split()
    start_hour, start_minutes = time.split(":")
    duration_hour, duration_minutes = duration.split(":")

    start_hour = int(start_hour)
    start_minutes = int(start_minutes)
    duration_hour = int(duration_hour)
    duration_minutes = int(duration_minutes)

    if duration_hour > 12:
        while i <= duration_hour:
            i += 1
            if i % 12 == 0:
                count += 1
                continue
        hours_left = duration_hour - (count*12)

        new_hour = start_hour + hours_left
        new_minute = start_minutes + duration_minutes

        if clock == 'PM':
            if new_hour >= 12:
                new_hour = new_hour - 12
                count = count + 1

                days_later = (count // 2) + 1

                if new_minute >= 60:
                    new_minute = new_minute - 60
                    new_hour = new_hour + 1
                    count = count + 1

                    days_later = (count // 2) + 1

                    if count % 2 == 0:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " " + clock \
                                   + " ({days_later} days later)".format(days_later=days_later)

                    else:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " AM" \
                                   + " ({days_later} days later)".format(days_later=days_later)

                else:
                    if count % 2 == 0:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " " + clock \
                                   + " ({days_later} days later)".format(days_later=days_later)

                    else:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " AM" \
                                   + " ({days_later} days later)".format(days_later=days_later)
            else:
                if new_minute >= 60:
                    new_minute = new_minute - 60
                    new_hour = new_hour + 1
                    count = count + 1

                    days_later = (count // 2) + 1

                    if count % 2 == 0:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " " + clock + " (next day)"

                    else:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " AM" \
                                   + " ({days_later} days later)".format(days_later=days_later)

        else:
            if new_hour >= 12:
                new_hour = new_hour - 12
                count = count + 1

                if new_minute >= 60:
                    new_minute = new_minute - 60
                    new_hour = new_hour + 1
                    count = count + 1

                    if count % 2 == 0:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " " + clock

                    else:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " PM"

                else:
                    if count % 2 == 0:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " " + clock

                    else:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " PM"
            else:
                if new_minute >= 60:
                    new_minute = new_minute - 60
                    new_hour = new_hour + 1
                    count = count + 1

                    if count % 2 == 0:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " " + clock

                    else:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " PM"

                else:
                    if count % 2 == 0:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " " + clock + " (next day)"

                    else:
                        new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " PM"

    else:
        new_hour = start_hour + duration_hour
        new_minute = start_minutes + duration_minutes

        if clock == "AM":
            if new_hour > 12:
                new_hour = new_hour - 12

                if new_minute >= 60:
                    new_minute = new_minute - 60
                    new_hour = new_hour + 1
                    new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " PM"

                else:
                    new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " " + clock

            else:
                if new_minute >= 60:
                    new_minute = new_minute - 60
                    new_hour = new_hour + 1
                    new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " PM"

                else:
                    new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " " + clock

        else:
            if new_hour > 12:
                new_hour = new_hour - 12

                if new_minute >= 60:
                    new_minute = new_minute - 60
                    new_hour = new_hour + 1
                    new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " AM"

                else:
                    new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " AM" + " (next day)"

            else:
                new_time = str(new_hour) + ":" + str(new_minute).zfill(2) + " " + clock

    return new_time        

**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0</code>.

**Challenge:** Time Calculator

**Link to the challenge:**
https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/time-calculator

@msalas2199,
I think you should take a look at answers to this question

1 Like

nice issue, I have already solved it lat year

My code has a lot of if statements as well but I feel it has a neater structure and is easier to read (my code). It could still do with some tiding up, I’ll need to go back over it bearing in mind the link in the previous reply.

emm am new in coding can u help me

You can post problem where you’re facing difficulty…
We’ll try to help you understand that…