(Python) How to find the number of occurrences in a list?
<p>Instead of using the .count function, I'm trying to determine the number of times a number input by a user occurs in a list. Appended to the originally empty list are 20 random integers between 1 and 10.</p>

<pre><code>import random

print()
num_list = []

for num in range(20):
    num_list.append(random.randint(1, 10))

chosen_num = eval(input("Enter a number between 1 and 10, inclusive: "))

occurences = num_list.count(chosen_num)

print()
print("The number " + str(chosen_num) + " occurs " + str(occurences) + " time(s) in the list ")
</code></pre>

<p>Is there any way I can use a "for" loop or if-else statement to determine the number of times a number appears in the list, rather than using .count?</p>

<p>Thanks.</p>

<p>It is very easy:</p>

<pre><code>occurences = 0
for item in num_list:
    if item == chosen_num:
        occurences += 1
</code></pre>

<p>So the full program looks like this:</p>

<pre><code>import random

print()
num_list = []

for num in range(20):
    num_list.append(random.randint(1, 10))

chosen_num = eval(input("Enter a number between 1 and 10, inclusive: "))

occurences = 0
for item in num_list:
    if item == chosen_num:
        occurences += 1

print()
print("The number " + str(chosen_num) + " occurs " + str(occurences) + " time(s) in the list ")
</code></pre>

python