Tell us what’s happening:
Hello. I’m back at certification stuff, and I’m trying to get python now. The time calculator’s logic is mostly done, but there’s a slight issue when counting how many days have passed, and will sometimes return one less than it should
Your code so far
print('\nTIME CALCULATOR\n')
def add_time(start, duration, day_of_week = '', days = 0, init_am_pm = False, recursion = 0):
#The extra parameters are just for recursion purposes
recursion_num = recursion
if not init_am_pm:
print(f'\n==========================\n\nSTART OF TIME OPERATION\n\n--------------------------\n\nMain calculation\n')
else:
recursion_num += 1
print(f'--------------------------\n\nRecursive calculation #{recursion_num}\n')
start_first_num = 0
start_last_num = 0
am_pm = '-\_(ツ)_/-'
days_of_the_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
day_of_the_week = ''
if len(start) == 8:
start_first_num = int(start[0] + start[1])
start_last_num = int(start[3] + start[4])
am_pm = start[6] + start[7]
else:
start_first_num = int(start[0])
start_last_num = int(start[2] + start[3])
am_pm = start[5] + start[6]
d_first_num = 0
d_last_num = 0
if len(duration) == 6:
d_first_num = int(duration[0] + duration[1] + duration[2])
d_last_num = int(duration[4] + duration[5])
elif len(duration) == 5:
d_first_num = int(duration[0] + duration[1])
d_last_num = int(duration[3] + duration[4])
else:
d_first_num = int(duration[0])
d_last_num = int(duration[2] + duration[3])
passed_days = days
print(f'Passed days: {passed_days}\n')
print(start_first_num, ':', start_last_num, am_pm, '\n')
print(d_first_num, ':' , d_last_num, '\n')
new_first_num = start_first_num + d_first_num
new_last_num = start_last_num + d_last_num
print(new_first_num, ':', new_last_num, '\n')
overflow = 0
while new_last_num >= 60:
new_last_num -= 60
overflow += 1
while new_first_num > 12:
new_first_num -= 1
overflow += 1
if am_pm == init_am_pm:
passed_days += 1
new_time = str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm
print(new_time, f', Plus {overflow} hours\n')
if overflow:
if new_first_num + overflow <= 12:
new_first_num += overflow
overflow = 0
else:
if am_pm == 'AM':
am_pm = 'PM'
else:
am_pm = 'AM'
overflow -= 12
if overflow < 0:
new_first_num += overflow
if new_last_num < 10:
new_last_num = '0' + str(new_last_num)
print(am_pm, init_am_pm, '\n')
initial_period = init_am_pm
if not init_am_pm:
initial_period = am_pm
if day_of_week:
day_of_the_week = day_of_week.title()
day_of_the_week = days_of_the_week[(days_of_the_week.index(day_of_the_week) + passed_days) % 7]
if day_of_the_week:
am_pm += ', ' + day_of_the_week
if passed_days == 1:
am_pm += ' (next day)'
elif passed_days > 1:
am_pm += f' ({passed_days} days later)'
new_time = str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm
print(new_time, f', Plus {overflow} hours\n')
if overflow > 0:
# D I V I N E R E C U R S I O N ! ! ! !
return add_time(str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm[0] + am_pm[1], f'{overflow}:00', day_of_the_week, passed_days, initial_period, recursion_num)
else:
return new_time
print(f"--------------------------\n\nEnd of time operation. Result is: [{add_time('11:59 PM', '24:05', 'Wednesday')}]\n")
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Challenge Information:
Build a Time Calculator Project - Build a Time Calculator Project