# Need some help with the Time Calculator Python Code

Hi everyone, I’ve recently picked up python and I need some help with this assignment.

The methodology i thought of for this would be to split the input arguments into Hours, Minutes and do the corresponding additions. For the minutes, while the end result is greater than 60, I did a looped subtraction so that the end result will remain within 60. The number of loops would then be added to the hours. For the hours, similarly, I created a while loop to subtract the number of hours in a day till its within hours.

Somehow this doesn’t pass all 12 tests. Would appreciate help to understand why!

def add_time(starttime, duration, weekdayvalue=“not specified”):

starttime.split()
time_numbers=starttime.split()[0]
AM_PM=starttime.split()[1]
time_hours=int(time_numbers.split(":")[0])
time_mins=int(time_numbers.split(":")[1])
duration_hours=int(duration.split(":")[0])
duration_mins=int(duration.split(":")[1])

weekdayvalue = weekdayvalue.lower()
daysinweek = [“monday”,“tuesday”,“wednesday”,“thursday”,“saturday”,“sunday”]

if weekdayvalue != “not specified”:
daysindex = daysinweek.index(weekdayvalue)

#convert 12H to 24H format
if AM_PM == “PM”:
time_hours = time_hours + 12

#calculating the new minutes number & normalising it to within 60 minutes.
time_mins=int(time_mins)+int(duration_mins)
while time_mins >= 60:
time_mins = time_mins -60
time_hours=time_hours+1

#calculating the new hour number
time_hours=time_hours+duration_hours
#calculating whether the new hours becomes a new day
i=0
while time_hours >= 24:
time_hours = time_hours - 24
i=i+1

#if i > 1, then its next day.
if i == 0:
dayspast = “”
if time_hours >12:
time_hours = time_hours - 12
AM_PM = “PM”

``````else:
time_hours = time_hours
AM_PM = "AM"
``````

elif i == 1:
dayspast = str("(next day)")

``````if weekdayvalue!="not specified":
daysindex= daysindex+1

if daysindex >7:
daysindex = 0
else:
daysindex=daysindex

if time_hours >12:
time_hours = time_hours - 12
AM_PM = "PM"

else:
time_hours = time_hours
AM_PM = "AM"
``````

elif i > 1:
dayspast = “({} days later)”.format(i)
if weekdayvalue != “not specified”:
daysindex= daysindex+i
while daysindex >7:
daysindex = daysindex - 8
else:
daysindex=daysindex
if time_hours > 12:
time_hours = time_hours - 12
AM_PM = “PM”
else:
time_hours = time_hours
AM_PM = “AM”

time_mins=format(time_mins,“02”)
time_print=""
time_print=str(time_hours)+":"+time_mins)

``````#print(str(time_numbers))
``````

if weekdayvalue != “not specified”:
return(time_print," “, AM_PM,”, “, daysinweek[daysindex].capitalize(),” “,dayspast)
else:
return(str(time_hours),”:",time_mins," ",AM_PM, dayspast)

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