write a python program to input a number and count the occurrence of a given number in a given list

huangapple go评论69阅读模式
英文:

write a python program to input a number and count the occurrence of a given number in a given list

问题

我需要一个答案这个问题
你们可以帮我吗

我尝试过这个

lst=[]
n = int(input("输入元素的数量:"))
for i in range(0, n):
    ele = int(input())
lst.count(ele)
print(lst)

无法得到问题的答案

英文:

I need an answer for this question
can u guys hel me

I tried this

lst=[]
n = int(input("Enter number of elements :"))
for i in range(0, n):
    ele = int(input())
lst.count(ele)
print(lst)

cant get answer for the question

答案1

得分: 1

这似乎是一个非常直接的解决方案。您只需遍历您给定的列表,并将每个元素与您想要检查的数字进行比较。只需在条件满足的情况下增加计数。

given_lst = [1, 2, 3, 4 , 5, 65, 6, 4, 5, 4 , 4, 6, 2]

num = int(input("输入一个数字: "))

count = 0
for number in given_lst:
    if number == num:
        count += 1

print(f"总出现次数: {count}")
英文:

Seems very straightforward solution to me. You just need to loop through your given list and compare each element with the number you want to check. Just increment the count in case of if condition satisfies.

given_lst = [1, 2, 3, 4 , 5, 65, 6, 4, 5, 4 , 4, 6, 2]

num = int(input("Enter a number: "))

count = 0
for number in given_lst:
    if number == num:
        count += 1

print(f"Total occurrence: {count}")

huangapple
  • 本文由 发表于 2023年2月10日 14:32:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定