Help: Scientific Computing with Python project 2

Hi,

So I am trying to complete the time calculator project and the code I wrote is working in pycharm on my computer but wont work in replit and I have no idea why. If someone could help that would be awesome, thank you!

Your code so far

def add_time(start_time, duration_time, weekday = ""):
    split_start_time = start_time.split()
    time = (split_start_time[0])
    time_split = time.split(":")
    hour_1 = time_split[0]
    minute_1 = time_split[1]
    am_pm = split_start_time[1]
    format_jk = ""



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


    duration_time_split = duration_time.split(":")
    hour_d = duration_time_split[0]
    minute_d = duration_time_split[1]

    total_minute = int(minute_d) + int(minute_1)

    if am_pm == "AM":
        total_hour = int(hour_1) + int(hour_d)

    else:
        total_hour = int(hour_1) + 12 + int(hour_d)

    total_time = str(total_hour) + ":" + str(int(total_minute)).zfill(2)

    if int(total_minute) > 60:
        total_hour = int(total_hour) + 1
        total_minute = int(total_minute) - 60

    days_passed = int((total_hour / 24))

    j = "(" + str(days_passed)
    k = " days later)"
  
    if days_passed >= 2:
        format_jk = str(j) + str(k)
        new_hour = int(total_hour) - 24 * int(days_passed)

        if new_hour == 0:
            total_time = str(12) + ":" + str(int(total_minute)).zfill(2)
            pa = "AM"
        elif new_hour < 12:
            pa = "AM"
            total_time = str(int(total_hour) - 24 * (int(days_passed))) + ":" + 
             str(int(total_minute)).zfill(2)
        elif new_hour == 12:
            total_time = str(int(total_hour) - 24 *(int(days_passed))) + ":" + 
             str(int(total_minute)).zfill(2)

            pa = "PM"
        else:
            pa = "PM"
            total_time = str(int(total_hour) - 24 * (int(days_passed))) + ":" + 
            str(int(total_minute)).zfill(2)

    elif days_passed >= 1:
        format_jk = "(next day)"
        if total_hour > 36:
            total_time = str(int(total_hour) - 36) + ":" + str(int(total_minute)).zfill(2)
        else:
            total_time = str(int(total_hour) - 24) + ":" + str(int(total_minute)).zfill(2)

        if total_hour >= 36:
            pa = "PM"
        else:
            pa = "AM"

    else:
        if total_hour >= 12:
            new_hour = int(total_hour) - 12
            if new_hour == 0:
                total_time = "12" + ":" + str(int(total_minute)).zfill(2)
            else:
                total_time = str(int(total_hour) - 12) + ":" + str(int(total_minute)).zfill(2)

            pa = "PM"

        else:
            pa = "AM"
            total_time = str(int(total_hour)) + ":" + str(int(total_minute)).zfill(2)

    if weekday:
        weekday = weekdays.index(weekday.title())
        new_weekday = int((weekday + days_passed) % 7)
        weekday = weekdays[new_weekday]

 format_1 = (total_time + " "+ pa + "," + " " + weekday)
    format_2 = (total_time + " " + pa)
    format_3 = (total_time + " " + pa + " " + format_jk)
    format_4 = (total_time + " " + pa + "," + " " + str(weekday).capitalize() + " " + format_jk)

    if format_jk == "":
        if weekday:
          return((format_1))
        else:
          return((format_2))
    elif weekday == "":
        return((format_3))
    else:
        return((format_4))

Challenge: Time Calculator

Link to the challenge:

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 (’).

Careful. print and return are very different. The function should return the required string, not just print it.

Thank you! I added a return statement into my code and again it is running just fine in pycharm but wont work in replit, it looks like some of the calculations dont work right in replit

What is your updated code?

I just added return in front of the print statement which then allowed for an actual error in replit which gave the wrong time but what is confusing me is the right time is showing up in pycharm. Thanks for the help!

Actually paste in your code please. Just describing what you changed is not useful.

Like @JeremyLT was saying return and print are different. Sometimes in the challenges when something should be printed, it may require your function to print a result and sometimes the result is printed by a different function - in which case the function you code should simply return the result without printing it. In this case the challenge says to “return the result.”

That’s the actual problem…
print() is a function that creates an output in the console, but has a return-value of None.
The test however looks at your return value and compares it to a string containing the actual solution. If you print or “return print()”, you function will return None, which is obviously different from a string.

1 Like

So I got the formatting to work and updated the my code so far part of this post, I still get three errors regarding the time output coming out as negative on some of them, but it still comes out fine in pycharm.

We cannot help you, if you don’t at least post your errors O.o

1 Like

Oops I was going to post it but I forgot. I just figured out that I accidentally had 36 instead of 24 in one of my sections

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