G Warner
G Warner

Reputation: 1379

Else statement invalid syntax

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

Upvotes: 0

Views: 610

Answers (2)

Bubai
Bubai

Reputation: 280

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?

Upvotes: 1

marsh
marsh

Reputation: 2730

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'

Upvotes: 2

Related Questions