Hi, new-ish to Python and having trouble sorting a syntax error. Here is the extract:
i=GPIO.input(PIR_PIN)
currentDT=datetime.datetime.now().time()
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”.
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.
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:
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.
i=GPIO.input(PIR_PIN)
currentDT=datetime.datetime.now().time()
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)
print(lastmotion)
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