Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python list methods comprehesion

The program takes a number x – the length of the list. Create a list and populate it from the console. After that, the number y will be entered, which may or may not exist in the list. Your task is to print how many times the number y occurs in the list.

Example:

input:
5 # is the number x
0 # start of list
5
15
1
2 # end of list
5 # is the number y
output: 1

My attempt:
But output is 0
Can you help me please?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x = int(input("x = "))
a = []
for _ in range(x):
    numbers = int(input("number = "))

y = int(input("y = "))
counter = 0
for i in a:
    if y in a:
        counter += 1
print(counter)

>Solution :

x = int(input("x = "))
a = []
for _ in range(x):
    number = int(input("number = "))
    a.append(number)  # add this line

y = int(input("y = "))
counter = 0
for i in a:
    if i == y:  # change condition here
        counter += 1
print(counter)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading