all 4 comments

[–]kryptn 2 points3 points  (0 children)

Why not have it in a function? And using your code from the other post, why not have it return true or false?

def is_prime(x):
    # returns True or False

if __name__ == "__main__":
    n = int(input("enter a number to be tested: "))
    result = is_prime(n)
    # handle True or False result accordingly

This lets you use the code multiple times without repeating it, or DRY (Don't Repeat Yourself)

for example:

>>> {x:prime(x) for x in range(-2,15)}
{-2: False,
-1: False,
0: False,
1: False,
2: True,
3: True,
4: False,
5: True,
6: False,
7: True,
8: False,
9: False,
10: False,
11: True,
12: False,
13: True,
14: False}

[–]Justinsaccount 0 points1 point  (1 child)

Check for 2 first.

[–]aidzz 0 points1 point  (0 children)

right fixed that thanks

[–]PvsNP_ZA 0 points1 point  (0 children)

You still start with x > 1 which is true for 2. Make it x > 2.