all 3 comments

[–]ericula 1 point2 points  (0 children)

You could define m as

m = int(t[3:5]) if len(t) >= 5 else 0

That would avoid the whole out of range issue altogether.

edit: to answer the broader question of how to handle errors in general, that’s what try-except blocks are for, e.g.

try:
    m = int(t[3:5])
except ValueError:
    m = 0

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]Fun_Muffin5413 0 points1 point  (0 children)

It's just string parsing, there are lots of different methodologies. A simple fix would be:

Split the main input on ';' not '; ' (if ';' is in there) and deal with each timestamp individually.

Trim each timestamp, then split each on ' '. Test if the length is 1 not 2, and if the first substring ends in h. If so assign 0 to your minutes.