0

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?

Adam
  • 709
  • 4
  • 16
  • Please use SOs code formatting. Also, google first, to find the other questions, that got answred on this already. – Aiyion.Prime Mar 22 '20 at 09:58
  • 1
    Does this answer your question? [Use a.any() or a.all()](https://stackoverflow.com/questions/34472814/use-a-any-or-a-all) – Aiyion.Prime Mar 22 '20 at 09:59
  • 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 Mar 22 '20 at 10:08

1 Answers1

0

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.

hmhmmm
  • 333
  • 1
  • 4