Trying to understand how to use the any() function
<p>I want to pick many given numbers and compare them to a number I chose, how do i do this using the <code>any</code> or <code>all</code> command , I tried this and it didn't work, any input would be appreciated: </p>

<pre><code>import random

v = int(input("What number are you looking for ?"))
a1 = int(input("What is the first number"))
a2 = int(input("What is the second number"))
a3 = int(input("What is the third number"))
a = random.choice([a1,a2,a3])
b = random.choice([a1,a2,a3])
c = random.choice([a1,a2,a3])
if any ([a, b, c]) == v:
   print('We got a hit')
</code></pre>

<p>Entering the following, I can't get the <code>if</code> to evaluate to <code>True</code>: </p>

<pre><code>What number are you looking for ?5
What is the first number1
What is the second number2
What is the third number5
&gt;&gt;&gt; 
</code></pre>

<p>How am I using <code>any</code> wrong here ? Since the last number is <code>5</code> I should of gotten a hit but I get nothing.</p>

<p>Because you're using <code>any</code> wrong. To achieve what you want, supply the condition to <code>any</code>:</p>

<pre><code>if any(v == i for i in [a, b, c]):
   print('We got a hit')
</code></pre>

<p>This will check that there's a value in the list <code>[a, b, c]</code> which equals <code>v</code>.</p>

<p>Your approach:</p>

<pre><code>any([a, b, c]) == v
</code></pre>

<p>Will first use <code>any</code> to check if any of the elements inside the iterable (<code>[a, b, c]</code>) supplied has a truthy value (and it does, all of them do if they're positive integers) and returns the appropriate result <code>True</code> indicating that. So:</p>

<pre><code>any([a, b, c])
</code></pre>

<p>will return <code>True</code>. Your condition then becomes:</p>

<pre><code>True == v
</code></pre>

<p>which obviously evaluates to <code>False</code>.</p>

python python-3.x