0

I can't seem to find a solution to this faulty code.

nt(time_input[:2]) = int(time_input[:2]) - 12

time_input has the value of time in the hh:mm format.

If the number exceeds 12:00, the number will be subtracted by 12 (am/pm).

I always get a syntax error like "Can't assign to function call." Thanks in advance!

Python learner
  • 1,159
  • 1
  • 8
  • 20
Onur T
  • 11
  • 1
  • 4
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 21 '21 at 08:22
  • You can't assign to the result of a function call. Also, if you remove the `int(...)` on the left-hand-side, you can't assign an `int` to a slice. What exactly is it you want to do, and what is `time_input`, a `str`? – tobias_k Oct 21 '21 at 08:28
  • @tobias_k Is there another way to do it? – Onur T Oct 21 '21 at 08:29
  • @tobias_k time_input is a string with the value of hh:mm (eg. 08:12). 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. – Onur T Oct 21 '21 at 08:33
  • Does this answer your question? [SyntaxError: "can't assign to function call"](https://stackoverflow.com/questions/5964927/syntaxerror-cant-assign-to-function-call) – wovano Oct 21 '21 at 18:42

3 Answers3

1

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.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

You need to test whether the HH part is greater than 12 and adjust accordingly. Here's how you could do that:

def fixtime(t):
    # assume that t is in the format HH:MM
    # no validation is performed
    hh = int(t[:2])
    return f'{hh-12:02d}{t[2:]}' if hh > 12 else t
0
time_input = "13:12" 
t_list = time_input.split(':')
str(int(t_list[0]) - 12) +':' + t_list[1] if int(t_list[0]) > 12 else time_input
Amrit Prasad
  • 373
  • 6
  • 18