什么不能分配给函数调用是什么意思(python)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27673885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does can't assign to function call mean (python)
提问by Dorian Dore
I have recently started learning Python, and I have been assigned the task of creating a function that averages grades in a dictionary.
我最近开始学习 Python,我被分配了创建一个函数来平均字典中的成绩的任务。
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
def average(grades):
total = 0
grades.sum() = total
total.float() = total
total = total / len(grades)
return total
However, when the function is executed, I receive the error, "Can't assign to function call" What does this mean, and how do I fix it?
但是,当函数执行时,我收到错误,“无法分配给函数调用”这是什么意思,我该如何解决?
回答by Sailesh Kotha
You are assigning a value to the return value of inbuilt function which is not possible..Try this
您正在为内置函数的返回值分配一个值,这是不可能的..试试这个
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
def average(grades):
total = 0
total = grades.sum()
total = total.float()
total = total / len(grades)
return total
回答by Jivan
Instead of this:
取而代之的是:
grades.sum() = total
You'd rather do that:
你宁愿这样做:
total = grades.sum()
If you want to dig deeper, here is an explanation of the whys and hows.
回答by PM 2Ring
I'm guessing that you want to pass a list of grades to your average()
function.
我猜您想将成绩列表传递给您的average()
函数。
You have the syntax a bit mixed up in grades.sum() = total
and total.float() = total
你的语法有点混乱grades.sum() = total
和total.float() = total
You (probably) want something like this:
你(可能)想要这样的东西:
def average(grades):
total = sum(grades)
return float(total) / len(grades)
And you'd call that function like this:
你会像这样调用这个函数:
avg = average(alice["homework"])
total = sum(grades)
says calculate the sum of the values in grades
and store the result in a variable named total
. The function call sum(grades)
asks a question, and in Python (like most other programming languages) we write the question on the rightside of the =
sign, and the place to put the answer goes on the leftside of the =
sign. But your code has that backwards.
total = sum(grades)
说计算中值的总和grades
并将结果存储在名为 的变量中total
。函数调用sum(grades)
提出一个问题,在 Python(与大多数其他编程语言一样)中,我们将问题写在符号的右侧=
,而放置答案的位置在=
符号的左侧。但是你的代码倒退了。