Robotics & Artificial Intelligence

Write a function which takes a number and a list as arguments and counts the occurrence of that number in the given list.

Python Functions

1 Like

Answer

def count_occurrence(num, lst):
    count = 0
    for x in lst:
        if x == num:
            count = count + 1
    return count

numbers = eval(input("Enter list: "))
num = int(input("Enter the number to count: "))

result = count_occurrence(num, numbers)
print("Occurrence:", result)

Output

Enter list: [2, 4, 5, 2, 7, 2, 9]
Enter the number to count: 2
Occurrence: 3

Answered By

1 Like


Related Questions