You can not assign to the result of a function call (int(...) = ...
), and neither you can assign an int
to the slice of a str
(or, in fact, anything, since strings are immutable). Instead, you could use a format string to create a new string with the updated time and assign that time time_input
:
>>> time_input = "13:45"
>>> f"{int(time_input[:2])-12:02}{time_input[2:]}"
'01:45'
Unless that's the only thing you want to do with that time, I'd strongly suggest parsing that string as a datetime.time
and manipulating that instead.
I have to convert these numbers to words. Eg. the value of time_input = 08:12 and i have to make a program that says what time it is in english. e.g It is twelve past eight.
In this case, both of the above is overkill. Instead, just get both the hours and minutes as integers first, and then subtract from the hours. No need to make that a string again.
>>> hrs, mns = map(int, time_input.split(":"))
>>> hrs %= 12
>>> hrs, mns
(1, 45)
Then, apply your "number to words" code to both, hrs
and mns
.