max函数 cannot assign to function call
时间: 2024-01-13 16:03:42 浏览: 113
The error message "cannot assign to function call" occurs when you try to assign a value to the result of a function call, which is not allowed in Python. The `max()` function returns the maximum value from a given iterable or a series of arguments.
Here's an example of the correct usage of the `max()` function:
```python
numbers = [1, 2, 3, 4, 5]
maximum = max(numbers)
print(maximum) # Output: 5
```
In this example, `max(numbers)` returns the maximum value from the list `numbers`, and it is assigned to the variable `maximum`.
If you need to update the value of a variable using `max()`, you can do it separately like this:
```python
a = 10
b = 5
maximum = max(a, b)
a = maximum # update the value of 'a' with the maximum value
print(a) # Output: 10
```
In this case, `max(a, b)` returns the maximum value between `a` and `b`, and it is assigned to the variable `maximum`. Then, the value of `a` is updated with the value of `maximum`.
阅读全文