为什么此代码会收到无效的语法错误?

时间:2019-03-14 19:53:57

标签: python python-3.x

def compare(A: object, B: object) -> object:
    if(A>B):
        return A
    elif(A<B):
        return B
    else:
        return A


if __name__ == "__main__":
    A = int(input())
    scorelist = []

    for i in range(0,A):
        tmp = int(input())
        scorelist.append(tmp)

    sum =+ int(scorelist[0])
    k = 1
    for j in A-2:
        D = compare(scorelist[k],scorelist[k+1]
        k = scorelist.index(D)
        sum =+ D


    print (sum)
    print(scorelist)

scorelist是未解决的参考,我不知道为什么不能使用“ k”。

   line 22
    k = scorelist.index(D)
    ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:0)

您缺少右括号:

def compare(A: object, B: object) -> object:
    if(A>B):
        return A
    elif(A<B):
        return B
    else:
        return A


if __name__ == "__main__":
    A = int(input())
    scorelist = []

    for i in range(0,A):
        tmp = int(input())
        scorelist.append(tmp)

    sum =+ int(scorelist[0])
    k = 1
    for j in A-2:
        D = compare(scorelist[k],scorelist[k+1])
        k = scorelist.index(D)
        sum =+ D


    print (sum)
    print(scorelist)