请求协助解决Python报错:can't assign to function call
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

