PTA平台7—-29

PTA 7-29 优异生查询(类和对象)
上传后是非零返回 ,是为什么?


n = list(input().split())
m = list(map(int,input().split()))
c = list(map(int,input().split()))
e = list(map(int,input().split()))
n0 = m[0]+c[0]+e[0]
n1 = m[1]+c[1]+e[1]
n2 = m[2]+c[2]+e[2]
if n0>n1>n2 or n0>n2>n1:
    print(n[0],end=' ')
    print(m[0],c[0],e[0],end='')
if n1>n0>n2 or n1>n2>n0:
    print(n[1],end=' ')
    print(m[1],c[1],e[1],end='')
if n2>n1>n0 or n2>n1>n0:
    print(n[2],end=' ')
    print(m[2],c[2],e[2],end='')

a>b>c这种形式的比较语句是有效的,但是在这个代码中,由于比较运算符的优先级问题,if n0>n1>n2 or n0>n2>n1:等语句实际上是被解释为(n0>n1)>n2 or (n0>n2)>n1,而不是n0>n1 and n1>n2,这会导致比较结果不符合预期。
参考一下这个

n = list(input().split())
m = list(map(int,input().split()))
c = list(map(int,input().split()))
e = list(map(int,input().split()))
n0 = m[0]+c[0]+e[0]
n1 = m[1]+c[1]+e[1]
n2 = m[2]+c[2]+e[2]
if (n0>n1 and n1>n2) or (n0>n2 and n2>n1):
    print(n[0],end=' ')
    print(m[0],c[0],e[0],end='')
if (n1>n0 and n0>n2) or (n1>n2 and n2>n0):
    print(n[1],end=' ')
    print(m[1],c[1],e[1],end='')
if (n2>n1 and n1>n0) or (n2>n0 and n0>n1):
    print(n[2],end=' ')
    print(m[2],c[2],e[2],end='')