Discussion about this post

User's avatar
VINOD VIJAYAN's avatar

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()

Expand full comment
Bijal's avatar

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

Expand full comment
3 more comments...

No posts