+ 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?
2 odpowiedzi
+ 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
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")