0
votes

This is the code that I have:

def largest(arr,n): 
    max = arr[0] 
    for i in range(1, n): 
        if arr[i] > max: 
            max = arr[i] 
    return max

It gives me this error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

What does this error mean and how can I solve it?

1
Please use SOs code formatting. Also, google first, to find the other questions, that got answred on this already.Aiyion.Prime
Does this answer your question? Use a.any() or a.all()Aiyion.Prime
You haven't shown the arguments you pass to your function, but I believe the first argument is a list of NumPy arrays rather than scalar values.Błotosmętek

1 Answers

0
votes

Generally, this error implies that you are comparing an array to a scalar value. Since you are doing comparison only on line if arr[i] > max:, it is safe to say that one of these (or both) is an array.

I was not able to reproduce your error as you didn't mention what are you passing as arr. Passing a one dimensional array works well. If you could elaborate a bit on how you are calling the function, I would be happy to look into that further.