Build a Time Calculator Project - Build a Time Calculator Project

Tell us what’s happening:

didnt implement the weekdays, but im really sure the test 3 and 6 must be broken, i really tried everything, i also recalculated step 6 it was false, so i think this is a issue or so?! Greetings from Germany luuull >->

Your code so far

def add_time(start, duration):
    time, period = start.split()
    left_side = time.split(':')
    print(left_side)
    right_side = duration.split(':')
    print(right_side)

    sum_hour = int(left_side[0]) + int(right_side[0])
    sum_min = int(left_side[1]) + int(right_side[1])
    




    if sum_min > 60:
        sum_hour += 1
        sum_min -= 60
    print('Die Minuten sind:',sum_min)
    print('Die Stunden sind:', sum_hour)
    
    day_later = ""
    days = sum_hour // 24
    if days == 1:
       day_later =' (next day)'
    if days > 1:
       days += 1
       day_later = f' ({days} days later)'
    print(f'Die Tage sind {days}')

    
    period_cycle = sum_hour // 12
    print(f'Period_cycle: {period_cycle}')

    while period_cycle > 0:
        # Wechsel zwischen AM und PM
        if period == 'AM':
            period = 'PM'
        else:
            period = 'AM'
        period_cycle -= 1

        
    
    def calculate_hour(sum_hour):
        if sum_hour < 12:
            return sum_hour  # Rückgabe des Werts, wenn sum_hour < 12
        if sum_hour >= 12:
            rest_hour = sum_hour - 12
        return calculate_hour(rest_hour)

          
    result_hour = calculate_hour(sum_hour)
    if result_hour == 0:
        result_hour = 12  
    print(f'Das ist endliche Stunde: {result_hour}')      

    
    formatted_min = str(sum_min).zfill(2) #Minuten auf 2 Stellen

    

    print(f'{result_hour}:{formatted_min} {period}{day_later}')
    return f'{result_hour}:{formatted_min} {period}{day_later}'

    

add_time('11:59 PM', '24:05')

Your browser information:

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

Challenge Information:

Build a Time Calculator Project - Build a Time Calculator Project

Consider how the (next day) and (x days later) is determined. For example, if the time is 10:15 PM, or (without AM/PM) 20:15 and added are 25 hours, is the resulting time next day? What about adding 5 hours to it?

i dont understand this, when you add 24 hours to it, and the lasting 1h it should be 11:15 PM, where does the 5h comes from?!

11:15 PM (next day) ?

What if you add 29 hours instead?

These are two random examples, other than to make a point (with the examples) the values themselves are not that significant. To some extent they could be picked differently.

ohhhkey i understand, but not really how to implement that

It helped me on this project to convert everything into minutes right away, and convert it back into hours:minutes at the end for output.

Don’t work with hours and minutes in the middle, it’s more complicated.

thats how i got it, its really more simple

1 Like