How to print prime numbers list in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to print prime numbers list in python?

Here's the code I have written that I think should be giving a list of prime numbers, but it is not working. Please give me suggestions so that I can fix this code. def is_prime(): lists = [2, 3,] x = 4 for n in range(2, x-1): if x % n != 0: lists.append(x) x += 1 return lists print(is_prime())

14th Sep 2017, 11:56 AM
Amandeep Singh
Amandeep Singh - avatar
7 Answers
+ 2
Here's the properly indented code... def is_prime(): lists = [2, 3,] x = 4 for n in range(2, x-1): if x % n != 0: lists.append(x) x += 1 return lists print(is_prime()) thanks for help..
14th Sep 2017, 3:00 PM
Amandeep Singh
Amandeep Singh - avatar
+ 1
That's great piece of code..and very efficient too👍.... but if you can tell me what 's wrong with my code that would be better..... I am grateful for your help
14th Sep 2017, 3:45 PM
Amandeep Singh
Amandeep Singh - avatar
+ 1
It's Ok...thanks for your help...I appreciate that☺️☺️
14th Sep 2017, 3:56 PM
Amandeep Singh
Amandeep Singh - avatar
0
I can't run your code... It's not well indented
14th Sep 2017, 2:45 PM
Paul Osakue
Paul Osakue - avatar
0
Here's mine def is_prime: for i in range(1,100): if i==3 or i==5 or i%2!=0 and i%3!=0 and i%5!=0: print(i) is_prime()
14th Sep 2017, 3:27 PM
Paul Osakue
Paul Osakue - avatar
0
Okay... I'll check it now
14th Sep 2017, 3:46 PM
Paul Osakue
Paul Osakue - avatar
0
Sorry I can't figure it out.... It's not just looking correct
14th Sep 2017, 3:55 PM
Paul Osakue
Paul Osakue - avatar