Else 语句无效语法

Else statement invalid syntax

我的代码在最后一个 else 语句(下面代码的倒数第二行)中抛出 SyntaxError: invalid syntax 错误。任何人都可以看到是什么原因造成的吗?我在 CentOS 上 运行 Python 2.7。

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

你不能这样做:

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

你必须这样声明:

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

进行此检查的更好方法是:

 if div[1].lower() == 'o'

另一种测试与多于 1 项的方法是:

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

How do I test one variable against multiple values?

所述