SyntaxError: "can't assign to function call"
This line:
invest(initial_amount,top_company(5,year,year+1)) = subsequent_amount
produces an开发者_开发百科 error:
SyntaxError: can't assign to function call
How do I fix this and make use of value of the function call?
Syntactically, this line makes no sense:
invest(initial_amount,top_company(5,year,year+1)) = subsequent_amount
You are attempting to assign a value to a function call, as the error says. What are you trying to accomplish? If you're trying set subsequent_amount
to the value of the function call, switch the order:
subsequent_amount = invest(initial_amount,top_company(5,year,year+1))
You wrote the assignment backward: to assign a value (or an expression) to a variable you must have that variable at the left side of the assignment operator ( = in python )
subsequent_amount = invest(initial_amount,top_company(5,year,year+1))
You are assigning to a function call:
invest(initial_amount,top_company(5,year,year+1)) = subsequent_amount
which is illegal in Python. The question is, what do you want to do? What does invest()
do? I suppose it returns a value, namely what you're trying to use as subsequent_amount
, right?
If so, then something like this should work:
amount = invest(amount,top_company(5,year,year+1),year)
In Python, if we put parenthesis after a function name, e.g, main()
, this indicates a function call, and its value is equivalent to the value returned by the main()
function.
The function-calling statement is supposed to get a value. For example:
total = add(1, 4)
#total = 5
And if we try to assign a value to the function call statement in Python, we receive the Syntax error.
add(1, 4) = total
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
In Python 3.10, we receive some extra information in the error that suggests that we may want to perform the comparison test using the ==
operator instead of assigning =
.
In this statement
invest(initial_amount,top_company(5,year,year+1)) = subsequent_amount
we can conclude two things:
1. illegal use of assignment operator. This is a syntax error when we assign a value or a value return by a function to a variable. The variable should be on the left side of the assignment operator and the value or function call on the right side.
Example
subsequent_amount = invest(initial_amount,top_company(5,year,year+1))
2. forget to put double == operators for comparison.
This is a semantic error when we put the assignment operator (=) instead of the comparison (==).
Example
invest(initial_amount,top_company(5,year,year+1)) == subsequent_amount
If you see no assignment on the line where is this error, then it might be caused by accidentally writing wrong for cycles such as sum(x for f())
or for f():
instead of sum(x for x in f())
or for x in f():
. This confusing error is probably caused by paraser first checking if the expression to be assigned to is valid and only after it checking if the for cycle is valid.
This can also happen in for cycles writen in correct format such as sum(x for f() in x)
or for f() in x:
精彩评论