+ 4

find prime no.

num=int(input("Num:")) if num>1: for i in range(2,num): if num % i==0: print("not prime") break elif num % i!=0: print("prime") else: print("invaid entry") I wanted to find prime no. Can you let me know what is wrong?

24th Jul 2021, 7:34 AM
Gouri Shinde
Gouri Shinde - avatar
2 Answers
+ 1
Gouri Shinde Here's a shorter version: print("invalid entry" * ((num := int(input())) < 2) or "prime" * (num == 2 or all(num % i for i in range(2, num))) or "not prime") # Hope this helps
24th Jul 2021, 3:05 PM
Calvin Thomas
Calvin Thomas - avatar
0
i think all you need to do is remove “break”. the code below was able to loop through all the numbers in the range. num=int(input("Num:")) if num>1: for i in range(2,num): if num % i==0: print("not prime") elif num % i!=0: print("prime") else: print("invaid entry")
30th Jul 2021, 9:39 PM
you are smart. you are brave.
you are smart. you are brave. - avatar