Home > OS >  Freecodecamp python time calculator not initiating tests
Freecodecamp python time calculator not initiating tests

Time:07-25

I've follow a tutorial writing the code below, however when I run the console just sits idle, if you couldn't tell, I'm new to cod ing, any guidance on where I've gone wrong would be appreciated - also tips welcome. here is the python code;

def add_time(start, duration, day_of_week=False):

  print(add_time)
  week_days_index = {"monday": 0, "tuesday": 1, "wednesday": 2, "thursday": 3, "friday": 4, "saturday": 5, "sunday": 6}

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

  duration_tuple = duration.partition(":")
  duration_hours = int(duration_tuple[0])
  print(duration_hours)
  duration_minutes = int(duration_tuple[2])
  print(duration_minutes)

  start_tuple = start.partition(":")
  start_minutes_tuple = start_tuple[2].partition(" ")
  start_hours = int(start_tuple[0])
  start_minutes = int(start_tuple_minutes[0])
  am_pm = start_tuple_minutes[2]
  ampm_flip = {"AM": "PM", "PM": "AM"}

  day_amount = int(duration_hours / 24)

  end_minutes = start_minutes   duration_minutes
  if(end_minutes >= 60):
    start_hours  = 1
    end_minutes = end_minutes % 60

  amount_ampmflips = int((start_hours   duration_hours) / 12)
  end_hours = (start_hours   duration_hours) % 12

  end_minutes = end_minutes if end_minutes > 9 else "0"   str(end_minutes)
  end_hours = end_hours = 12 if end_hours == 0 else end_hours
  if (am_pm == "PM" and start_hours   (duration_hours % 12) >= 12):
    day_amount  = 1

  am_pm = ampm_flip[am_pm] if amount_ampmflip % 2 == 1 else am_pm

  returntime = str(end_hours)   ":"   str(end_minutes)   " "   am_pm
  if (day_of_week):
    day_of_week = day_of_week.lower()
    index = int((week_days_index[day_of_week])   day_amount) % 7
    new_day = week_days_array[index]
    returntime  = ","   new_day

  if (day_amount == 1):
    return returntime   " "   "(next_day)"
  elif(day_amount > 1):
    return returntime   " ("   str(day_amount)   " days later)"

  return returntime

CodePudding user response:

don't worry, got it. I think it had to done with Replit's configurable run command, I must of mess with it. Tips still welcome.

CodePudding user response:

I tried out your code and ran into some typos that you might want to correct in case anyone else reviews this issue looking for solutions. In your code above, you have a variable named "start_minutes_tuple" that is later referred to as "start_tuple_minutes". Also, there is a variable named "amount_ampmflips" that is later referred to as "amount_ampmflip". Both of these caused errors when testing. Also, it took a bit of guessing as to the format of the parameters to enter to make a test call to your function. You might want to add that in.

With those bits, I did construct a test program that I believe follows the spirit of your project.

def add_time(start, duration, day_of_week=False):

  print(add_time)
  week_days_index = {"monday": 0, "tuesday": 1, "wednesday": 2, "thursday": 3, "friday": 4, "saturday": 5, "sunday": 6}

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

  duration_tuple = duration.partition(":")
  duration_hours = int(duration_tuple[0])
  print(duration_hours)
  duration_minutes = int(duration_tuple[2])
  print(duration_minutes)

  start_tuple = start.partition(":")
  start_tuple_minutes = start_tuple[2].partition(" ")
  start_hours = int(start_tuple[0])
  start_minutes = int(start_tuple_minutes[0])
  am_pm = start_tuple_minutes[2]
  ampm_flip = {"AM": "PM", "PM": "AM"}

  day_amount = int(duration_hours / 24)

  end_minutes = start_minutes   duration_minutes
  if(end_minutes >= 60):
    start_hours  = 1
    end_minutes = end_minutes % 60

  amount_ampmflip = int((start_hours   duration_hours) / 12)
  end_hours = (start_hours   duration_hours) % 12

  end_minutes = end_minutes if end_minutes > 9 else "0"   str(end_minutes)
  end_hours = end_hours = 12 if end_hours == 0 else end_hours
  if (am_pm == "PM" and start_hours   (duration_hours % 12) >= 12):
    day_amount  = 1

  am_pm = ampm_flip[am_pm] if amount_ampmflip % 2 == 1 else am_pm

  returntime = str(end_hours)   ":"   str(end_minutes)   " "   am_pm
  if (day_of_week):
    day_of_week = day_of_week.lower()
    index = int((week_days_index[day_of_week])   day_amount) % 7
    new_day = week_days_array[index]
    returntime  = ","   new_day

  if (day_amount == 1):
    return returntime   " "   "(next_day)"
  elif(day_amount > 1):
    return returntime   " ("   str(day_amount)   " days later)"

  return returntime

# Added this in to perform a test of the function - it should print out 9:00 PM
  
new_time = add_time("10:00 AM","11:00")
print(new_time)

If you would, let me know if I followed the spirit of your project. All the best in your future coding.

Regards.

  • Related