from time_calculator import add_time
# from unittest import main
print(add_time("11:06 PM", "2:02"))
# Run unit tests automatically
# main(module='test_module', exit=False)

My time_calculator.py file:
Here I’m separating the hours from the minutes in the function call. The second part of this is where I’m running in to a problem. I’m trying to separate the hours part of the duration time (everything before the colon in 2:02). I tried iterating over the characters in string “2:02”, but I can’t seem to figure out how to separate out everything before the colon, in this case, the ‘2’. I then need to add it to the start_hours, but that shouldn’t be too hard if I can isolate the 2. Any help is appreciated, thank you!

def add_time(start, duration):
# start => hours:minutes AM/PM
start_hours = 0
start_minutes = 0
if start[1] != ':':
start_hours = int(start[0]) + int(start[1])
start_minutes = start[3] + start[4]
elif start[1] == ':':
start_hours = start[0]
start_minutes = start[2] + start[3]
# duration is hours:minutes
# hours => whole number
# minutes => 00 - 59
# iterate over the hours in duration
duration_hours = 0
for duration_hours in duration:
added_hours = duration_hours
# print(duration_hours)
# print(added_hours)
if duration_hours == ':':
break
start_hours + added_hours
print(start_hours)

you can do str.split(':') which will return a list of strings, derived of the string you split, using " : " as delimiter.
PS: use it twice, once to split the first argument into “11:06” and “PM” and once to split the numbers into “11” and “06”. You can also define a function, to take a string like “11:06” and return it in minutes, so you can call that function for both the start time and add time and avoid code repetition(within it, you want to split the string into hours and minutes)

@Sylvant I’m struggling with how to approach this problem. I started simple by adding the duration hours and minutes to the starting hours and minutes, like this:

start hours and minutes: 11:06
duration hours and minutes: 2:02
new time: 13:08

However I’m stuck on converting the new time to a 12-hour format and including an ‘AM’ or ‘PM’ after the new time. I need to convert the new time shown above from 13:08 to 1:08 PM. I have an idea on how to convert to the 12-hour format, but I don’t think it will work for any given duration of hours. Could you provide any guidance? Thank you!

Consider using a counter variable to determine AM or PM. For example if more than 12 hours have elapsed counter +=1 and you can do some if /elif statements to determine if it’s AM/PM (even or odd), and I would recommend using the modulo operator (%) for that. Once you determine AM/PM you can just add that AM/PM string onto your number. Hope that helps.

This is really helpful, I figured I needed to use a counter variable to track the hours, but my brain couldn’t crank out how I should use it. Thank you!

Hi.
What i did was, i checked in the tag is “AM” or “PM” and in the later case, id add 12 hours to the start value, as we calculate the day in 24 hours pattern.
After i sum up the initial time and additional time, i check the number of days it would add(if you store the time in minutes, you divide it by 1440- the minutes in a day, use // to get value without reminder). Once i know the days, i subtract their value from the time(time%1440- this will give the reminder). Then, again by using // and % i get the hours and minutes, which i am going to use for the returned result. At this point you can start parse your numbers to strings and build your result. Dont forget to add zero in front of minutes, if they are less than 10.
The day part was tricky to adjust, but not that complicated. I used an array(list) to store the week days names and if the input included a day, id check the position of that day and using the additional days i gained from the extra time(if any) id calculate at which index of the array is the resulted day(if you find the calculations here too complicated to figure out, i can help you).

I need some additional help, I’m not sure what you mean when you add 12 hours to the start value. I can check whether it’s “AM” or “PM” but I’m not sure why you add 12 hours. Thank you for the help!

Hi
the difference between 1am and 1pm is 12 hrs. If we add 12 hrs to 1am, it will become 1pm. But we cant really use am and pm for arithmetic calculations, we need to refer to them as numbers. Pm practically adds +12 hours, so we turn 1pm into 13(hours) to use in our calculations. 1am is unchanged, we add nothing to am values, they are explicit on what they represent.
Getting back to the project problem, if we call add_time("5:00 AM", "10:00"), the returned time should be 3 PM(no extra days), but if we call add_time("5:00 PM", "10:00"), we must return 3 AM and one day later. So in order to turn this into arithmetical problem i can work on, i parse PM hours +12. Then i can use that number and add the 10 extra hours(parameter 2). The result is 27 hours, or one day and 3 hours, which can be translated to 3 AM and one day, to match the expected function result.
I hope this explanation makes it a bit more clear.

I am also new on Python. My advise to you is, use your knowledge and logic from JS(i suppose you did the JS curriculum before that?), think how you would solve the problem with JS code, then write that logic into Python code. In both cases you use very similar methods, iterate by lists, working with some math problems and manipulate strings.

def add_time(start, duration):
am_or_pm = start.split(' ')[1]
pm = 12
# break up start hours and mins
s_hrs = start.split(':')[0] # 5
s_mins_and_pm = start.split(':')[1] # 00 PM
s_mins = s_mins_and_pm.split(' ')[0] # 00
# start total in minutes
s_total_in_mins = int(start.split(':')[0]) * 60 + int(s_mins) # 300
# break up duration hours and minutes
d_hours = duration.split(':')[0] # 3
d_mins = duration.split(':')[1] # 00
# duration total in minutes
d_total_in_mins = int(d_hours) * 60 + int(d_mins) # 180
# total up hours, PM, and duration time to equal days
# if PM, add 12 hours
if am_or_pm == 'PM':
total_s_d_mins = s_total_in_mins + d_total_in_mins + pm * 60 # 1200
print(total_s_d_mins)
else: # am_or_pm == 'AM'
total_s_d_mins = s_total_in_mins + d_total_in_mins
print(total_s_d_mins)
# return new_time

I’m having trouble with how I should approach combining the duration and start times and then converting them to the 12-hour format. I’m hoping you can provide a couple tips on the next steps, and I really appreciate it!

have i not gave directions on how to combine the times? Like i said, you want, to first convert both values(start time and time to add) into minutes, a common unit to work with.
For example we convert ‘10:30 AM’ to 10*60+30. And 3:10 time to add we convert to 3*60+10. We get two values, 630 and 190. We add them together as that the challenge requires us to do. We end up with 820(minutes). This is the minutes representation of the time we must return. We want to convert it to the format the tests requires. So we first extract the days. How many days(later) the time is. One days is 1440 minutes. 820//1440 returns 0. So we dont get extra days for our solution. Then we check the hours. 820//60 tells us the hour(s) we need to return are(is) 13. We subtract 13 hours from 820, to get the minutes we need to return. 820-(13*60)=40 . So 13 hours and 40 minutes. Now we need to parse these values into the string format the challenge expects. 13 hours will becomes 1 PM(whenever over 12 hours, we must return PM value, otherwise we return AM). The solution will be 1:40 PM. This is the logic, you should be able to work thru the code part. There are some details i saved. Overall to parse the values you get into the expected string the sequence is hours+':'+'minutes'+ ' AM/PM'(+' days later')