You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助解决Python报错:can't assign to function call

Fixing the "can't assign to function call" Error in Your Python List Operation

Hey there, let's get this sorted out quickly. That error pops up because you're trying to do something Python doesn't allow: assign a value to the result of a function call.

Let's look at the problematic line in your code:

int(l[i])=i*int(m[i])-int(m[j])

Here, int(l[i]) is a function call—it takes the value of l[i] and returns its integer equivalent. You can't assign a new value to that returned result; you need to assign the computed value back to the actual list element l[i] instead.

Corrected Code

Here's the fixed version of your code:

for i in range(n):
    for j in range(i+1):
        # Compute the value, convert to int, then assign to l[i]
        l[i] = int(i * int(m[i]) - int(m[j]))
print(l)

A Cleaner, More Efficient Version

If your m list contains string representations of numbers, you can convert the entire list to integers upfront to avoid repeating the int() calls inside the loops. This makes the code easier to read and faster:

# Convert all elements in m to integers first
m_as_ints = [int(item) for item in m]

for i in range(n):
    for j in range(i+1):
        l[i] = int(i * m_as_ints[i] - m_as_ints[j])
print(l)

Why This Works

Instead of trying to assign to int(l[i]) (which is a temporary value returned by the int() function), we now calculate the arithmetic result, convert it to an integer, and assign that final value directly to l[i]—a valid, mutable position in your list. That's exactly what Python expects for assignments.

内容的提问来源于stack exchange,提问作者raju rama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:08:35