Time Calculator / Python

Tell us what’s happening:
I feel like I’ve written really dumb code. It won’t even run, have I made an unending loop somewhere?

Your code so far

def add_time(start, duration, *day):
    split_start = []
    start_hour = ""
    start_minutes = ""

    if "AM" in start:
      first_am_pm = "AM"
    else:
      first_am_pm = "PM"

    split_start = start.split(":")
    start_hour = split_start[0]
    start_minutes = split_start[1].split(" ")

    duration_split = duration.split(":")
    duration_hours = int(duration_split[0])*60
    duration_minutes = int(duration_split[1])
    dur = duration_minutes + duration_hours

    hours = int(dur/60)
    if hours == (dur/60):
      minutes = 0
    else:
      minutes = (dur/60 - int(dur/60))*60
    
    final_hours = int(start_hour) + hours
    final_minutes = int(start_minutes[0]) + minutes

    if final_minutes > 60:
      final_minutes -= 60
      final_hours += 1

    days_passed = 0

    while final_hours > 23:
      days_passed += 1
      final_hours -= 24

    if days_passed == 0:
      passed = ""
    elif days_passed == 1:
      passed = "(next day)"
    else:
      passed = "(",days_passed,"days later)"

    week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

    if final_hours >= 12:
      final_hours -= 12
      final_hours = "0" + str(final_hours)
      am_pm = "PM"
    else:
      final_hours = str(final_hours)
      am_pm = "AM"

    if day != None:
      sum_days = 0

      start = 7
      while sum_days <= days_passed:
        for i in week:
          keep_date = i
          
          if i == day:
            start = i
          if start == i:
            sum_days += 1

      new_time = week[keep_date] + final_hours + ":" + final_minutes + am_pm + " " + passed
    else:
      new_time =final_hours + ":" + final_minutes + am_pm + " " + passed

    return new_time

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0.

Challenge: Time Calculator

Link to the challenge:

If you think you got an infinite loop - comment out a loop and check if the code is running then.
Also throw in some print-commands and maybe comments to keep track of what you and the code are doing.

It’s propably something with the while-for-loop, though I don’t really understand what it is doing.
But it will run into a problem because “for i in week” with week=[“Monday”, …] means i will have the values “Monday”…
You write that into keep_date and then adress “week[keep_date]” - which could be something like "week[“Monday”] which throws an error because the bracket notation only works with integers.

Well, you’re right about what you said
but anyway I had many more problems after all.
I decided to restart, thanks for answering.

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