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.
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
Related Questions
Write a function which returns if a number passed as as argument is a perfect number or an Armstrong number.
Write a function which returns if a number passed as an argument is a prime or composite number.
Write a function which counts and displays the number of vowels, consonants, uppercase, and lowercase characters in a string entered as input.
Write a function which deletes all the negative values from a list passed as an argument.