def add_time(start, duration ,day=0):
weekdays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
day_count = 0
day_went = 0
if day != 0:
day_count = weekdays.index(str(day).lower())
meridiem = start[len(start) - 2:len(start)]
new_minutes = int(start[len(start) - 5:len(start) - 3]) + int(duration[len(duration) - 2:])
new_hours = int(start[:len(start) - 6]) + int(duration[:len(duration) - 3])
while new_minutes >= 60:
new_minutes -= 60
new_hours += 1
while new_hours >= 12:
new_hours = new_hours - 12
if meridiem == "PM":
meridiem = "AM"
day_count += 1
day_went += 1
else:
meridiem = "PM"
if new_hours == 0:
new_hours = 12
while day_count > 6:
day_count -= 7
if len(str(new_minutes)) == 1:
new_minutes = "0" + str(new_minutes)
if day_went == 0 and day == 0:
new_time = f'{new_hours}:{new_minutes} {meridiem}'
elif day_went == 0 and day != 0:
new_time = f'{new_hours}:{new_minutes} {meridiem}, {weekdays[day_count].capitalize()}'
elif day_went == 1 and day == 0:
new_time = f'{new_hours}:{new_minutes} {meridiem} (next day)'
elif day_went == 1 and day != 0:
new_time = f'{new_hours}:{new_minutes} {meridiem}, {weekdays[day_count].capitalize()} (next day)'
elif day_went > 1 and day == 0:
new_time = f'{new_hours}:{new_minutes} {meridiem} ({day_went} days later)'
elif day_went > 1 and day != 0 :
new_time = f'{new_hours}:{new_minutes} {meridiem}, {weekdays[day_count].capitalize()} ({day_went} days later)'
return new_time
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
Please don’t post solution code like this to the forum.
I’ve moved this to code feedback here for now, but you should have some more information. Do you have any questions about it?