0

My code is throwing a SyntaxError: invalid syntax error at the last else statement (second to last line in code below). Can anyone see what is causing this? I'm running Python 2.7 on CentOS.

def mintosec(time):
    foo = time.split(':')
    if re.match('o|O',foo[0]) == True: #check to see if any zeros are incorrectly labled as 'o's and replace if so
            div = list(foo[0])
            if div[0] == 'o' or 'O':
                    new = 0
            else:
                    new = div[0]
            if div[1] == 'o' or 'O':
                    new1 = 0
            else:
                    new1 = div[1]
            bar = int(str(new)+str(new1))*60
    else:
            bar = int(foo[0]) * 60
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
G Warner
  • 1,309
  • 1
  • 15
  • 27
  • 2
    You are making a classical mistake in your `if` conditions, see [How do I test one variable against multiple values?](https://stackoverflow.com/q/15112125) – Martijn Pieters Mar 17 '15 at 18:07
  • 1
    Are you sure you are running the code in your post? There is no syntax error here. There are *other* problems, just not syntax errors. You *would* get a syntax error if you unbalanced the parentheses on the preceding line (put one too many `(` on there, or miss out on a `)`), but that's not the case with the code you posted here. – Martijn Pieters Mar 17 '15 at 18:08
  • The interpreter sees `div[0] == 'o'` as one condition, and `'O'` as another condition. Either the first one will be true, or it will be false and the interpreter will try the second one, which will always be true. A non-empty string evaluates to `True` and an empty string evaluates to `False`. – TigerhawkT3 Mar 17 '15 at 18:10

2 Answers2

2

You cannot do:

if div[0] == 'o' or 'O':
    new = 0

You must declare it like:

if div[1] == 'o' or div[1] == 'O':
    new1 = 0

A better way to do this check would be:

 if div[1].lower() == 'o'
marsh
  • 2,592
  • 5
  • 29
  • 53
  • Ah yes you are correct about the syntax error. I may have misinterpreted the problem. Though as you stated in your comment there is no syntax error. – marsh Mar 17 '15 at 18:09
  • 1
    If this is the problem then the post is a duplicate. In this case I'd use `div[1].lower() == 'o'`, by the way. – Martijn Pieters Mar 17 '15 at 18:10
  • As would I, I am never sure though if I should be suggesting coding guidelines or simply helping them with their error. Is there a stack overflow section on this? – marsh Mar 17 '15 at 18:11
  • 1
    This change actually corrected the syntax error. It must have been causing a syntax error further down like having a missing quote messes thing up later in your code. Thank you! – G Warner Mar 17 '15 at 18:11
  • OK I see, thanks. While we're at it, can you see what is wrong with my regular expression? I'm trying to launch the `if` statement if foo[0] contains either 'o' or 'O' but it keeps passing straight to the else statement. Is `re.match()` not the appropriate function? Is my expression, `'o|O'` incorrect? – G Warner Mar 17 '15 at 18:47
  • Are you sure foo[0] from foo = time.split(':') is returning the right value? – marsh Mar 17 '15 at 18:51
  • Yes, in this case `foo = ['oo','oo']` – G Warner Mar 17 '15 at 18:58
1

another way to test vs more than 1 item is:

if div[1] in {'o', 'O'}:
    # stuff.

as described in How do I test one variable against multiple values?

Community
  • 1
  • 1
Bubai
  • 270
  • 1
  • 8