Python Time Calculator Project Feedback

Hi all!

I’m working on completing my first certification and I’ve just completed the python time calculator. I accomplished the goal - passing all the tests, but I know there were probably 100 better ways to get there. I was hoping to get some specific feedback on where I could optimize.

• my variable names are crap - I ended up with too many similar things and ran out of ways to differentiate them.

Here’s the link to my project: https://replit.com/@AbbyNeiman/boilerplate-time-calculator#time_calculator.py
Here’s my code:

``````def add_time(start, duration, *WD):
#split out the time
time = start.split(" ")

#split minutes and hours
spltime = time[0].split(":")

#convert to intergers
hr = int(spltime[0])
min = int(spltime[1])

#figure out military time
if "PM" in start:
milhr = hr + 12
else:
milhr = hr

#split duration into hours and minutes
durspl = duration.split(":")
durhr = int(durspl[0])
durmin = int(durspl[1])

milendhr = milhr + durhr
milendmin = min + durmin

#convert minutes over 60 to hours
if milendmin > 60:
endhr = milendhr + (milendmin // 60)
endmin = milendmin - (60 * (milendmin // 60))
else:
endmin = milendmin
endhr = milendhr

if endhr > 24:
#figure out days later (n)
n = endhr // 24
#convert into 1 day
nexendhr = endhr - (n * 24)

#convert out of military time
else:
n = 0
nexendhr = endhr
if nexendhr > 12:
newendhr = nexendhr - 12
else:
newendhr = nexendhr
#set 0 hour as 12
if newendhr == 0:
newendhr = 12

#final AM or PM
STOD = time[1]
if nexendhr < 12:
TOD = "AM"
else:
TOD = "PM"

if WD:
DOW = [
"monday", "tuesday", "wednesday", "thursday", "friday", "saturday",
"sunday"
]
for item in WD:
#standardize capitalizaiton of WD
day = item.lower()
#set listday as number of day on DOW list that WD starts as
listday = DOW.index(day)
endday = listday + n
# if the span is greater than a week, reset the days after 7
if n > 7:
Lday = endday % 7
enddow = DOW[Lday]
else:
enddow = DOW[endday]

#format the responses if WD is populated
if n == 1:
new_time = f"{newendhr}:{endmin:0>2d} {TOD}, {enddow.capitalize()} (next day)"
elif n > 1:
new_time = f"{newendhr}:{endmin:0>2d} {TOD}, {enddow.capitalize()} ({n} days later)"
else:
new_time = f"{newendhr}:{endmin:0>2d} {TOD}, {enddow.capitalize()}"
#format the responses without WD
else:
if n == 1:
new_time = f"{newendhr}:{endmin:0>2d} {TOD} (next day)"
elif n > 1:
new_time = f"{newendhr}:{endmin:0>2d} {TOD} ({n} days later)"
else:
new_time = f"{newendhr}:{endmin:0>2d} {TOD}"

return new_time
``````

I’ve edited your post 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 (').

1 Like

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