27

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?

0

5 Answers 5

23

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))
8

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))
0
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)
9
  • Then you must have a literal like a number or a string (in quotes) on the left-hand side of the assignment. Mar 24, 2019 at 13:56
  • then I can not put the value neither on the left nor on the right ! that will be an infinite cycle !! Mar 24, 2019 at 14:32
  • The name (variable) goes on the left, the value on the right. This is the same across practically all programming languages. Mar 24, 2019 at 14:48
  • yes !! doing that , I get : “can't assign to function call” Mar 24, 2019 at 15:04
  • You must be having a function call on the left side (something like foo(bar, baz) = 123. There may only be names, possibly tuples, on the left side. Please show the line that‘s causing the error. Mar 24, 2019 at 15:08
1

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
0

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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.