Hello! I’m just beginning to learn code and would like some help with how I can make my code more legible in a professional environment for my peers (i.e. declaring variable names). Also is there any way to make my code more efficient? I looked/fixed for any repeating code and tried to make it as concise as possible from my knowledge. Any input would be of great value!
My Code passed all tests. This is merely for improvement. Please request if my output is necessary.
def add_time(start, duration, day=False):
hr_str1, scnd_hlf = start.split(':')
mn_str1, meridiem = scnd_hlf.split()
hr_str2, mn_str2 = duration.split(':')
if day:
day_week = day
else:
day_week = ''
hr_int1, mn_int1, hr_int2, mn_int2 = int(hr_str1), int(mn_str1), int(hr_str2), int(mn_str2)
if meridiem == 'PM': hr_int1 + 12
ttl_hr = hr_int1 + hr_int2
ttl_mn = mn_int1 + mn_int2
days = 0
halfdays = 0
new_hr = new_mn = ''
new_day = ''
daylist = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
if ttl_mn >= 60:
ttl_hr += (ttl_mn // 60)
ttl_mn = ttl_mn % 60
if ttl_hr >= 24:
days += (ttl_hr // 24)
if ttl_hr == 24:
ttl_hr = 12
else:
ttl_hr = ttl_hr % 24
if ttl_hr >= 12:
if meridiem == 'AM':
meridiem = 'PM'
if ttl_hr != 12:
ttl_hr = ttl_hr - 12
elif meridiem == 'PM':
meridiem = 'AM'
days += 1
if ttl_hr != 12:
ttl_hr = ttl_hr - 12
new_hr = str(ttl_hr)
if ttl_mn <= 9:
new_mn = str(ttl_mn).rjust(2,'0')
else:
new_mn = str(ttl_mn)
for i in daylist:
if day_week.lower() == i.lower():
new_day = daylist[(daylist.index(day_week.title()) + days) % 7]
if days == 1:
new_time = "{}:{} {}{}{}{}".format(new_hr, new_mn,meridiem, ',' if new_day != '' else '', new_day.rjust(len(new_day)+1) if new_day != '' else '', (' (' + 'next' + ' day' + ')'))
else:
new_time = "{}:{} {}{}{}{}".format(new_hr, new_mn,meridiem, ',' if new_day != '' else '', new_day.rjust(len(new_day)+1) if new_day != '' else '', (' (' + str(days) + ' days later' + ')') if days >= 1 else '')
return new_time