SyntaxError: “can't assign to function call”

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

This line in my program:

invest(initial_amount,top_company(5,year,year+1)) = subsequent_amount 

causes me to get this error:

SyntaxError: can't assign to function call 

How do I fix this and make use of value of the function call?

回答1:

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


回答2:

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


回答3:

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) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!