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

What can I do to add a list and sort out all the prime numbers in the list? Python 3

I am creating a program that

  • accepts an inputted list
  • finds all the prime numbers and only displays them.

I tried many different methods, many derived from existing prime filters, but they have hardcoded lists rather user-inputted ones.

I just can’t seem to get a filter working with inputting a list, then filtering the prime numbers.

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

my_list = input("Please type a list")

list(my_list)

prime=[]
for i in my_list:
    c=0
    for j in range(1,i):
        if i%j==0:
            c+=1
    if c==1:
        prime.append(i)
return (prime)

>Solution :

When you get input, you’re getting a string. You can’t cast a string to a list immediately. Maybe you can request the user to use a separator between the numbers then use split method and cast strings to integers like this:

my_list = input("Please enter the list of numbers and use space seperator")

s_list = my_list.split()

cast_list = [int(num) for num in s_list]

Then, you can work on your prime number task based on your preferred algorithm.

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