When I run this code and give input as 25 it should return me its not a prime num, But when I debug the code the range values are not iterating into if condition, only the first value of the range is passed and if its not == 0 it moves to the else part.
JavaScript
x
12
12
1
def find(x):
2
if x > 1:
3
for i in range(2,x):
4
if x % i == 0:
5
return "its not a prime num"
6
else:
7
return "Its a prime num"
8
9
10
user = int(input("Enter your no: "))
11
print(find(user))
12
Please help me why its working like this , I am new to programming . TIA
Advertisement
Answer
As stated in a comment, this is an easy fix. Simply move the else
statement’s return
to outside of the loop.
JavaScript
1
11
11
1
def find(x):
2
if x > 1:
3
for i in range(2,x):
4
if x % i == 0:
5
return "its not a prime num"
6
return "Its a prime num"
7
8
9
user = int(input("Enter your no: "))
10
print(find(user))
11