Project Description
Your task for today is to build a Python program that counts how many times a specific number appears in a list. But instead of using a pre-defined list, your program should ask the user to enter the list and the target number via the terminal.
The user will type in the list as comma-separated numbers, like 3, 4, 5, 3, 2, 3
, and your program should process that input and determine how many times their chosen number appears.
This kind of number-counting logic is used in many real-world situations: from analyzing how many times a value appears in survey responses, to counting specific occurrences in datasets or log files. Learning how to clean and process user input will serve you well in any kind of data-focused project.
Expected Output
The program should prompt the user twice:
💡 Hint
Not sure how to start? Click the button below to get a head start with the first line of code and guidance on how to clean the user’s input.
𝌣 Solution
🔒 This solution is available to paid subscribers only.
✅ Two different ways to solve it! One uses .count()
, the other uses a manual loop.
🚀 Want to keep leveling up?
Browse 200+ projects at dailypythonprojects.substack.com/archive and unlock all solutions by subscribing. Build real Python skills daily and transform how you learn — one project at a time.
I wrote like this:
def count_occurrences():
lst = input("Enter a list of numbers separated by commas: ").split(",")
lst = [int(num) for num in lst]
target = input("Enter the number you want to count: ")
count = lst.count(target)
print(f"The number {target} appears {count} times in the list.")
count_occurrences()
I noticed that the count() function can take a string as input. So, I'm just curious—why would we need to convert the input to an integer when counting occurrences? Is there a specific case where this is necessary?
I have used like this
user_list = input("Enter a list of numbers seprated by commas")
for num in user_list:
number_list = user_list.split(",")
number_list.append(num)
user_input = input("Enter the number you want to count: ")
number_appered = number_list.count(user_input)
print(f"The number {user_input} appears {number_appered} times in the list")
Please let me know
Thanks