I have a task in which i need to calculate the runtime of a marathon. I'm given this as a start point
start_hour = 3
start_minute = 48
length = 172
Basically the start is at 3:48 and it continues for 172 minutes. My task is to find when the marathon ends.The endtime should look like this format 3:48 with minutes and hour converted to string and put together with ":". I have spent like 1 and a half hour and i still can't manage to solve it. This is what i have come out with:
endhour = start_hour + [length // 60]
endminute = start_minute + [length % 60]
end_minutee = endminute % 60
format[endhour]
endhourAsStr = str[endhour]
end_minuteeAsStr = str[end_minutee]
print[endhourAsStr + ":" + end_minuteeAsStr]
But when i print the final hour is shorter then it should be with 1 hour. I'm guessing that i need to do something with the > or < but i really can't figure it out.I guess that i only need that final push. offtopic: I'm very new to proggraming i don't have experience whatsoever.
asked Aug 14, 2017 at 11:42
You can use some datetime
trickery to get a guaranteed correct result:
start_hour = 3
start_minute = 48
length = 172
start = datetime.datetime[100, 1, 1, start_hour, start_minute, 0]
end = start + datetime.timedelta[minutes=length]
result = str[end.time[]]
If you want to get rid of the :00
seconds at the end, simply modify the last line:
result = end.strftime['%H:%M']
I prefer this approach because it accounts for edge cases like starting one day near midnight and ending in the next day.
answered Aug 14, 2017 at 11:50
4
I would suggest
endhour = start_hour + [length // 60]
endminute = start_minute + [length % 60]
endhour += endminute // 60
endminute = endminute % 60
endhour = endhour % 24
print['{}:{}'.format[endhour, endminute]]
After initializing the end hour and minute as you did, you extract again the minutes and hours from the end minutes.
Finally, you adjust the hours to be between 0 and 23.
answered Aug 14, 2017 at 11:46
UrielUriel
15k6 gold badges24 silver badges46 bronze badges
How about keeping everything in minutes and then working out the output as intended later? Keeping it simple to understand and read :]
start_time = start_hour*60 + start_minute
end_time = start_time + length
end_hour, end_minute = end_time // 60, end_time % 60
print['{}:{}'.format[end_hour, end_minute]]
# 6:40
answered Aug 14, 2017 at 11:50
shad0w_wa1k3rshad0w_wa1k3r
12.6k8 gold badges65 silver badges88 bronze badges
Use divmod
.
start_hour = 3
start_minute = 48
length = 172
elapsed_hours, elapsed_minutes = divmod[length, 60]
extra_hour, finish_minute = divmod[start_minute + elapsed_minutes, 60]
finish_hour = start_hour + elapsed_hours + extra_hour
print["{hour}:{minute}".format[hour=finish_hour, minute=finish_minute]]
This assumes that you don't expect anyone to finish after midnight/the next day. If you want to account for that possibility you can take the modulus of finish_hour
with 24.
answered Aug 14, 2017 at 12:07
BatmanBatman
8,3077 gold badges39 silver badges76 bronze badges
let's keep it simple
hour = int[input["Starting time [hours]: "]]
mins = int[input["Starting time [minutes]: "]]
dura = int[input["Event duration [minutes]: "]]
print[[[[hour*60+mins+dura]//60]%24] , ":" , [[mins+dura]%60] , sep=""]
or
hour = int[input["Starting time [hours]: "]]
mins = int[input["Starting time [minutes]: "]]
dura = int[input["Event duration [minutes]: "]]
endmins = [mins+dura]%60
endhours = [[hour*60 + mins + dura]//60]%24
print[endhours , ":" , endmins , sep=""]
answered Apr 19, 2020 at 12:09
Keeping it simple! :]
hour = int[input["Starting time [hours]: "]]
mins = int[input["Starting time [minutes]: "]]
dura = int[input["Event duration [minutes]: "]]
endmin=[mins+dura]%60
endhour=[[[hour*60]+mins+dura]//60]%24
print[endhour,":",endmin]
answered Mar 23, 2020 at 14:19
1
Keep it short and simple
st_hour = int[input["What's the start time in hours ?"]]
st_min = int[input["What's the start time in mins ?"]]
du_time = int[input["What's the duration time in mins ?"]]
end_hour = [st_hour + [st_min + du_time]//60]%24
end_mins = [st_min + du_time]%60
print[]
print["The end time = ", end_hour, ":", end_mins]
answered Dec 10, 2020 at 15:27
hour = int[input["Starting time [hours]: "]]
mins = int[input["Starting time [minutes]: "]]
dura = int[input["Event duration [minutes]: "]]
new_hour=int[[hour+round[[dura/60]]]%12]
new_mins=int[[mins+[dura%60]]%60]
print[new_hour,":",new_mins]
Suraj Rao
29.1k11 gold badges95 silver badges100 bronze badges
answered Feb 12 at 8:05
1
start_hour = int[input["Starting time [hours]: "]]
start_minute = int[input["Starting time [minutes]: "]]
length = int[input["Event duration [minutes]: "]]
#Calculating hours and minutes in python
end_hour = [start_hour + [[start_minute + length]//60]]%24
end_mins = [start_minute + length]%60
print[end_hour,end_mins,sep=":"]
This may helps
answered Feb 17 at 22:11
1
I suggest the following,
endhour=[start_hour*60+start_minute+length]%1440
endhour//=60
endmin=[start_minute+length]%60
print[endhour,":",endmin]
think is much simple solution :]
answered Apr 11, 2020 at 23:13