(Nov-06-2022, 02:45 AM)deanhystad Wrote: I would write like this:It's ok,but in first post he try to print all number given from user input.
Think is cleaner to have a
is_prime
function,then last part of code is same as his first post with added
is_prime
.def is_prime(p: int) -> bool: for n in range(2, p): #for n in range(2, int(p ** 0.5) + 1): # Faster if p % n == 0: return False return True if __name__ == '__main__': user_input = int(input("choose a number: ")) for x in range(2, user_input+1): if is_prime(x): print(f"{x} A prime,divide only by 1 and {x}") else: print(f"{x} Not prime")
Output:λ python prime.py
choose a number: 20
2 A prime,divide only by 1 and 2
3 A prime,divide only by 1 and 3
4 Not prime
5 A prime,divide only by 1 and 5
6 Not prime
7 A prime,divide only by 1 and 7
8 Not prime
9 Not prime
10 Not prime
11 A prime,divide only by 1 and 11
12 Not prime
13 A prime,divide only by 1 and 13
14 Not prime
15 Not prime
16 Not prime
17 A prime,divide only by 1 and 17
18 Not prime
19 A prime,divide only by 1 and 19
20 Not prime