收到错误消息:未定义名称“a”

时间:2021-05-02 15:25:32

标签: python-3.x

def sum(a, b, c, d):
    result = 0
    result = result+a+b+c+d
    return result


def length():
    return 4


def mean(a, b, c, d):
    return float(sum(a, b, c, d))/length()


print(sum(a, b, c, d), length(), mean(a, b, c, d))

我收到错误消息 name 'a' is not defined

2 个答案:

答案 0 :(得分:1)

如果你不定义变量,你会得到这些名称错误。例如,假设您在调用这些函数时切换了值 -

print(sum(a, b, c, d), length(), mean(a, b, c, d))

在这里,在这种情况下,您将得到 name b is not defined,因为 Python 解释器不知道变量 b 存储的值是什么。

你需要告诉解释器这些变量的值是什么。

For example - a=10, b=2,.. and so on

答案 1 :(得分:0)

变量未定义意味着您需要为该变量定义(赋值)。例如,a=1 应该可以工作。