Scientific computing with python project - Time Calculator

Tell us what’s happening:
Describe your issue in detail here.
so i’ve been able to build the function “add_time” for the time calculator project. i’m having an issue incorporating the 3rd parameter which is optional. the issue i have is how to check for the 3rd parameter, which represents days of the week and also where do i nest whatever conditional statement i use to check for the current day of the week if it’s provided.
thanks

Your code so far
def add_time(start, duration):
days=[“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”];
starthr=start.split(":")[0]
s=start.split(":")[1]
startmin=s.split(" “)[0]
dhour=duration.split(”:")[0]
dmin=duration.split(":")[1]
AMPM=s.split(" “)[1]
newtime=”"
nhr=int(starthr)+int(dhour)
nmin=int(startmin)+int(dmin)
if nhr<24:
if nhr>12 and nmin>=60 and AMPM==“AM”:
nhr=nhr-11
nmin=nmin-60
if len(str(nmin))==1:
newtime=str(nhr)+":"+“0”+str(nmin)+" “+“PM”
else:
newtime=str(nhr)+”:"+str(nmin)+" "+“PM”

    elif nhr>12 and nmin<60 and AMPM=="AM":
        nhr=nhr-12
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"PM"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"PM"
    elif nhr<12 and nmin<60 and AMPM=="PM":
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"AM"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"AM"
    elif nhr<12 and nmin>=60 and AMPM=="AM":
        nhr=nhr+1
        nmin=nmin-60
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"PM"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"PM"
    elif nhr>12 and nmin>=60 and AMPM=="PM":
        nhr=nhr-11
        nmin=nmin-60
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"AM" +" (next day)"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"AM"
    elif nhr<12 and nmin>=60 and AMPM=="PM":
        nhr=nhr+1
        nmin=nmin-60
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"AM"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"AM"
    elif nhr<12 and nmin<60 and AMPM=="AM":
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"PM"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"PM"
    elif nhr>12 and nmin<60 and AMPM=="PM":
        nhr=nhr-12
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"AM"+" (next day)"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"AM"+" (next day)"
    elif nhr==12 and nmin>=60 and AMPM=="AM":
        if len(str(nmin))==1:
            newtime=str(nhr+1)+":"+"0"+str(nmin-60)+" "+"PM"
        else:
            newtime=str(nhr+1)+":"+str(nmin-60)+" "+"PM"
    elif nhr==12 and nmin<60 and AMPM=="PM":
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"AM"+" (next day)"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"AM"+" (next day)"
    elif nhr==12 and nmin>=60 and AMPM=="PM":
        if len(str(nmin))==1:
            newtime=str(nhr+1)+":"+"0"+str(nmin-60)+" "+"AM"+" (next day)"
        else:
            newtime=str(nhr+1)+":"+str(nmin-60)+" "+"AM"+" (next day)"
    elif nhr==12 and nmin<60 and AMPM=="AM":
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"PM"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"PM"
elif nhr>24:
    nhr=int(starthr)+(int(dhour)%24)
    ndays=int(dhour)/24
    if nhr>12 and nmin>=60 and AMPM=="AM":
        nhr=nhr-11
        nmin=nmin-60
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"PM " + str(int(ndays+1))+" days later"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"PM " + str(int(ndays+1))+" days later"
    elif nhr>12 and nmin<60 and AMPM=="AM":
        nhr=nhr-12
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"PM " + str(int(ndays+1))+" days later"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"PM " + str(int(ndays+1))+" days later"
    elif nhr<12 and nmin<60 and AMPM=="PM":
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"AM " + str(int(ndays+1))+" days later"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"AM " + str(int(ndays+1))+" days later"
    elif nhr<12 and nmin>=60 and AMPM=="AM":
        nhr=nhr+1
        nmin=nmin-60
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"PM " + str(int(ndays+1))+" days later"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"PM " + str(int(ndays+1))+" days later"
    elif nhr>12 and nmin>=60 and AMPM=="PM":
        nhr=nhr-11
        nmin=nmin-60
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"AM " +" (next day)" + str(int(ndays+1))+ " days later"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"AM " + str(int(ndays+1))+ " days later"
    elif nhr<12 and nmin>=60 and AMPM=="PM":
        nhr=nhr+1
        nmin=nmin-60
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"AM " + str(int(ndays+1))+" days later"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"AM " + str(int(ndays+1))+" days later"
    elif nhr<12 and nmin<60 and AMPM=="AM":
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"PM " + str(int(ndays+1))+" days later"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"PM " + str(int(ndays+1))+" days later"
    elif nhr>12 and nmin<60 and AMPM=="PM":
        nhr=nhr-12
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"AM "+" (next day)" + str(int(ndays+1))+" days later"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"AM "+" (next day)" + str(int(ndays+1))+" days later"
    elif nhr==12 and nmin>=60 and AMPM=="AM":
        if len(str(nmin))==1:
            newtime=str(nhr+1)+":"+"0"+str(nmin-60)+" "+"PM " + str(int(ndays+1))+" days later"
        else:
            newtime=str(nhr+1)+":"+str(nmin-60)+" "+"PM " + str(int(ndays+1))+" days later"
    elif nhr==12 and nmin<60 and AMPM=="PM":
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"AM "+" (next day)" + str(int(ndays+1))+" days later"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"AM "+" (next day)" + str(int(ndays+1))+" days later"
    elif nhr==12 and nmin>=60 and AMPM=="PM":
        if len(str(nmin))==1:
            newtime=str(nhr+1)+":"+"0"+str(nmin-60)+" "+"AM "+" (next day)" + str(int(ndays+1))+" days later"
        else:
            newtime=str(nhr+1)+":"+str(nmin-60)+" "+"AM "+" (next day)" + str(int(ndays+1))+" days later"
    elif nhr==12 and nmin<60 and AMPM=="AM":
        if len(str(nmin))==1:
            newtime=str(nhr)+":"+"0"+str(nmin)+" "+"PM " + str(int(ndays+1))+" days later"
        else:
            newtime=str(nhr)+":"+str(nmin)+" "+"PM " + str(int(ndays+1))+" days later"
return newtime

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 GLS/90.10.4909.10

Challenge: Time Calculator

Link to the challenge:

By making it optional. How you do that? Well you implement it with a default value and check if the actual value is different from the default value.

As for where you put that check → that’s a question for the logic of your program. Propably somewhere around the area where you calculated the days passed.

Though… your code seems extremly hard to read. That’s how many elif?
I’ll be honest, you should seriously consider reworking that. This looks like code that is a nightmare to maintain and bugfix.

Like… do you have cases for PM and AM, where you just change the PM and AM in the output - instead of just using the actual AMPM variable which contains exactly that information?
That’s just tons of repeated code and the “where to put it” question in this design would propably result in 20 different positions…

Even if this code somehow works, that’s nothing you could ever write on the job ^^°

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