数组语法错误

Array Syntax Error

它说我有一个语法错误。但我看不出有什么问题。它位于显示 a.append 的行上。我很困惑。我很抱歉我是新手。我需要协助。我很确定我有不止一个。但是第一个在 a.append(0)

def invalidsyntax(a):
    if max(a)>20:
        print("The highest number is in position", a.index(max(a)))
        a.remove(max(a))
    if min(a)>5:
        print("The smallest number is", (min(a))), index.append(min(a)
        a.append (0)
    if min(a)>5
        a.sort(['a'])
        sum('a') / float(len('a')
        a[2]
        a[-3]          

TL;DR:您至少缺少一个括号,如何正确放置括号取决于您使用的是 Python 2 还是 Python 3。


数你的括号数:

print("The smallest number is", (min(a))), index.append(min(a)
     ^                          ^   ^ ^^^
     |                          |   |_|||
     |                          |______||
     |__________________________________|

在 Python 2 或 Python 3 中,语法错误是 index.append(min(a)).

缺少括号

但是,仅提供括号并不能完全解决您的问题。 Python 2 和 Python 3 对结果行的解释略有不同。假设你这样写:

print("The smallest number is", (min(a))), index.append(min(a))

在 Python 2 中,您有一个 print 语句 提供了两个表达式:元组 ("The smallest number is", min(a)) ([= 周围的括号17=] 是多余的)和对 index.append 的调用。每个表达式的值打印在同一行上,由 space 分隔。由于 index.append 总是 returns None,所以输出是

# Assuming min(a) returns 9
("The smallest number is", 9) None

在 Python 3 中,您有一个表达式语句,包括对 print("The smallest number is", min(a)) 的调用(同样,min(a) 周围的括号是多余的)和对 [=18 的调用=]. print 函数 in Python 3. 现在输出只是

The smallest number is 9

表达式的值是元组(None, None)(两个函数的return值。)