# Hows my code (its python prime number finder)

**URL:** <https://discuss.python.org/t/hows-my-code-its-python-prime-number-finder/109422>\
**Category:** Python Help\
**Created:** [October 10, 2026, 12:20am UTC](https://discuss.python.org/t/hows-my-code-its-python-prime-number-finder/109422 "2026-10-10T00:20:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![THE-8BIT-KING](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@THE-8BIT-KING](https://discuss.python.org/u/THE-8BIT-KING)\
**Post date:** [October 10, 2026, 12:20am UTC](https://discuss.python.org/t/hows-my-code-its-python-prime-number-finder/109422/1 "2026-10-10T00:20:19Z")

</div>

```python
username = input("What do you want your username to be? ")

def is_prime(number):
    if number < 2:
        return False
    if number % 2 == 0:
        return number == 2

    divisor = 3
    while divisor * divisor <= number:
        if number % divisor == 0:
            return False
        divisor += 2

    return True

def prime_finder():
    try:
        number = int(input("Choose a number: "))
    except ValueError:
        print("ERR: not a valid number")
        return

    if is_prime(number):
        print(number, "is prime")
    else:
        print(number, "is not prime")

def end():
    print("Goodbye,", username)

while True:
    print("prime-finder.py v1.0")
    print("1. prime finder")
    print("2. quit")

    try:
        answer = int(input("Make your choice: "))
    except ValueError:
        print("ERR: invalid input")
        continue

    if answer == 1:
        prime_finder()
    elif answer == 2:
        end()
        break
    else:
        print("ERR: invalid input")

```

---

<div class="post-metadata">

**Author:** ![jonathandung](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonathandung/32/35459_2.png) [@jonathandung](https://discuss.python.org/u/jonathandung)\
**Post date:** [October 10, 2026, 1:37am UTC](https://discuss.python.org/t/hows-my-code-its-python-prime-number-finder/109422/2 "2026-10-10T01:37:33Z")

</div>

This is good for a beginner, but be warned that it isn’t performant. Besides, “prime finder” is a misleading name when this program only checks if the input is prime.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [October 10, 2026, 1:38am UTC](https://discuss.python.org/t/hows-my-code-its-python-prime-number-finder/109422/3 "2026-10-10T01:38:18Z")

</div>

I would re-write it as:

```python
while True:
 
    print("prime-finder.py v1.0")
    print("1. prime finder")
    print("2. quit")

    try:
        answer = int(input("Make your choice: "))
        
        if answer == 1:
         prime_finder()
         break
         
        elif answer == 2:
         end()
         break
         
        else:
         print("ERR: invalid input")
            
    except ValueError:
        print("ERR: invalid input")
        continue

```

---

<div class="post-metadata">

**Author:** ![jonathandung](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonathandung/32/35459_2.png) [@jonathandung](https://discuss.python.org/u/jonathandung)\
**Post date:** [October 10, 2026, 1:38am UTC](https://discuss.python.org/t/hows-my-code-its-python-prime-number-finder/109422/4 "2026-10-10T01:38:49Z")

</div>

You should move the first print outside the loop.

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [October 10, 2026, 7:53am UTC](https://discuss.python.org/t/hows-my-code-its-python-prime-number-finder/109422/5 "2026-10-10T07:53:51Z")

</div>

> [@jonathandung](#):
>
> it isn’t performant

How would you make it performant?

---

<div class="post-metadata">

**Author:** ![jonathandung](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonathandung/32/35459_2.png) [@jonathandung](https://discuss.python.org/u/jonathandung)\
**Post date:** [October 10, 2026, 8:43am UTC](https://discuss.python.org/t/hows-my-code-its-python-prime-number-finder/109422/6 "2026-10-10T08:43:45Z")

</div>

What I meant was that OP should not expect this algorithm to be competitive against advanced algorithms such as ECPP for larger inputs, but the program in its current state is fair. There is also an optimization that can be made without introducing significant complexity; since primes larger than 3 are all of the form 6k + 1 or 6k - 1 for some positive integer k, after checking 2 and 3, the increments at each iteration can follow a cycle beginning from 1, namely (4, 2, 4, 2, …). This can be extended to a bigger basis such as (2, 3, 5) and the cycle starting from 1 would be (6, 4, 2, 4, 2, 4, 6, 2, …).

---

<div class="post-metadata">

**Author:** ![davidism](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidism/32/3295_2.png) [@davidism](https://discuss.python.org/u/davidism)\
**Post date:** [October 13, 2026, 2:12pm UTC](https://discuss.python.org/t/hows-my-code-its-python-prime-number-finder/109422/12 "2026-10-13T14:12:01Z")

</div>

Closed on October 13, 2026, 2:12pm UTC


