Python3 variable syntax error

Hi, new-ish to Python and having trouble sorting a syntax error. Here is the extract:

if currentDT.hour == 15 and currentDT.minute == 51 and currentDT.second == 0:
print (“Just a heads-up”)
# motionxhour = (re.findall(‘[0-9]+’, motionx))
motionxhour = int(list((filter(str.isdigit, motionx))[0])
lastmotion = (currentDT.hour-motionxhour)
print lastmotion

motionx is defined elsewhere as a string and the variable motionxhour returns the correct variable. I then want to calculate the difference between currentDT.hour (15, as 2nd line) and motionxhour. However, when I run the script, I get a syntax error, with the arrow pointing at the beginning of “lastmotion”.

Any help appreciated


What does the message of the syntax error say? Perhaps that’s a hint…

edit: at least, in python 3.10 it is a pretty good hint. There have been improvements to error messages in the more recent versions.

My guess is that you should be using print(lastmotion)

The error returned is:

File “/home/enss/”, line 31
lastmotion = (currentDT.hour-motionxhour)
SyntaxError: invalid syntax

I replaced the print command as suggested, but doesn’t get past line 31

The preceding code line is missing a close paren:

motionxhour = int(list((filter(str.isdigit, motionx))[0]))

Or indeed had an extra one before “filter”.

Many thanks, sorted


1 Like

My apologies for being coy. In python 3.10 the syntax error tells you that the parentheses in the preceding line haven’t been closed, but you are using a version that pre-dates this helpful message.

RaspberryPi not moved to 3.10 yet :slight_smile:

1 Like

Further question please - the above returns only the first digit of what is a two-digit number; what do I need to change to get the two digits please?

I would suggest that the issue is with the [0] slice, but without the detail, it’s just my best guess.

What I would do, is to ‘breakout’ the details of the code line, or maybe even process it in steps, rather than a single code line.

The full string of motionx is hh:mm, and I only want the hours. If I leave off the [0], I get both hours and minutes as digits.

Try [0:2] and see if that does what you need.

To add: if you’re using the [0] as an index, rather than a slice, you may get an index error at some point, but again I’m guessing, because I don’t have enough detail.

An example of how I would code a hr/min grab from a string object:

h, m = '11:00'.split(':')

Thanks - I have changed the calc to the one you suggested and it prints the correct result. I have hard-coded the figure “23” into the lastmotion calc, but now I get an error that says:

"TypeError: unsupported operand type(s) for -: ‘int’ and ‘str’

This suggests I think that the “split” function is returning a string? - even though there are no quotes around it when it prints out.

if currentDT.hour == 18 and currentDT.minute == 18 and currentDT.second == 0:
   print ("Just a heads-up")
   # motionxhour = (re.findall('[0-9]+', motionx))
   h, m = motionx.split(':')
   motionxhour = h
   print (motionxhour)
   #motionxhour = int(list(filter(str.isdigit, motionx))[0,2])
   lastmotion = (23-motionxhour)

this is a) commented out and b) incorrect. indexing with [0,2] doesn’t work here, and Rob was suggesting [0:2] (note the colon, not comma) to take the first two character in the string. If uncommented and fix, it should extract the hour digits from the string containing the time. edit: But then you will have a list of characters, not a single string containing the full number (which would be suitable for passing to int)

I think in the end, you will not be happy with this method of extracting the hour. Consider if the time is "1:20". If you filter the string for digits and take the first two, you will have ["1", "2"], but you just wanted the first one.

Yes. You’ll need to do a type conversion on the motionxhour = h, so motionxhour = int(h), but I am making some assumptions here; that is I’m assuming that the type conversion will always be valid.

If there’s a chance that its not going to be valid, there’s a couple of ways to handle that: one being a try/except branch.

I tried the [0:2] suggestion, but it comes up with a list error. So yes, commented out as it doesn’t work. I am expecting the digits to be always a two-digit number.

I am working on Rob’s other method for extracting the digits

Bingo, thanks Rob.

You’re welcome, anytime.