-1

[enter image description here][1]So the problem is i want to add 2 given input times, say for example - time1 = 19 50 (i want to add the time in this specific format) time2 = 02 20

i have this one code but it only works for certain time inputs,

x=[int(e) for e in input().split()]
y=[int(e) for e in input().split()]
hrs=x[0]+y[0]
min=x[1]+y[1]
if min>=60: 
    min=min-60 
    hrs=hrs+1
if hrs>=24: 
    hrs=hrs-24
print(hrs,min)

what do you think the solution can be.

update: Display number with leading zeros

After some intense Thonnying, (Thonny) I got the result to be printed with 01 in hrs using the zfill() functions. here is the updated code.

x=[int(e) for e in input().split()]
y=[int(e) for e in input().split()]
hrs=x[0]+y[0]
min=x[1]+y[1]
if min>=60: 
    min -= 60 
    hrs += 1
if hrs>=24: 
    hrs -= 24 
print(f"{str(hrs).zfill(2)} {min}")

But unfortunately, the test cases are not getting passed in tech gig code gladiator codeathon. Thanks for the help guys, noobie here is happy.

  • It seems to work fine to me... What is the problem? Please give an input for which it doesn't work, as well as real and expected output – Lecdi May 27 '22 at 07:23
  • `timedelta(hours=x[0], minutes=x[1]) + timedelta(hours=y[0], minutes=y[1])` – deceze May 27 '22 at 07:23
  • Or, if it's supposed to express a *point in time* + a duration: `datetime(hour=x[0], minute=x[1]) + timedelta(hours=y[0], minutes=y[1])` – deceze May 27 '22 at 07:24
  • So I found out what I want, actually the results are not coming in 2 digits especially the first "hh" part when i use the 24 hr format it should display 01 instead of 1.??? any ideas – Sreenivasan May 27 '22 at 11:37

0 Answers0