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?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Richee Chan
  • 263
  • 1
  • 4
  • 5

5 Answers5

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))
Brian Clapper
  • 25,705
  • 7
  • 65
  • 65
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))
Riccardo Galli
  • 12,419
  • 6
  • 64
  • 62
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)
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Then you must have a literal like a number or a string (in quotes) on the left-hand side of the assignment. – Tim Pietzcker Mar 24 '19 at 13:56
  • then I can not put the value neither on the left nor on the right ! that will be an infinite cycle !! – FabioSpaghetti Mar 24 '19 at 14:32
  • The name (variable) goes on the left, the value on the right. This is the same across practically all programming languages. – Tim Pietzcker Mar 24 '19 at 14:48
  • yes !! doing that , I get : “can't assign to function call” – FabioSpaghetti Mar 24 '19 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. – Tim Pietzcker Mar 24 '19 at 15:08
  • Thank you very much, I add those lines but you might not know the module, it's to run a software called femap from python: import Pyfemap app = Pyfemap.model(existObj) app = Pyfemap.model(existObj) feProp = app.feProp feProp.pval(7)= 10 this is not possible cause feProp.pval(7) is already a value – FabioSpaghetti Mar 24 '19 at 22:23
  • I think you should ask a new question for this with an explanation of what that code is supposed to be doing. If you have any links to the Pyfemap documentation, those would be handy, too. One stab in the dark: If `feProp.pval` is a list, then `feProp.pval[7] = 10` might work. Note the square brackets. – Tim Pietzcker Mar 25 '19 at 08:31
  • just in case, Stack has banned me from posting new questions I don't know how to ask now !! – FabioSpaghetti Mar 25 '19 at 08:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190605/discussion-between-tim-pietzcker-and-fabiospaghetti). – Tim Pietzcker Mar 25 '19 at 08:51
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
khelwood
  • 55,782
  • 14
  • 81
  • 108
Vinay Khatri
  • 312
  • 1
  • 8
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:

Jiří
  • 202
  • 2
  • 9