Prime code not working
<p>I am trying to learn some code, and found python. I'm only a beginner but intrested in a programming language.</p>

<p>I have some trouble writing my first program. I am trying to make a program that ask the user for a number, then checks if the number is a prime number, if it is, it will add it to the prime number list.</p>

<p>here is my code.</p>

<pre><code>prime = [2,3,5,7]
get = int(input("what number do you want to check ? "))

a = 0
while a &lt; len(prime):
        print("Test " + str(a+1))
        test = (get / prime[a])
        if isinstance(test, int):
            print(str(get) + " is not a prime number" )
            break
        else:
            a += 1

else:
    prime.extend(get)
    print(str(get) + " is a prime number, prime number added to list"
</code></pre>

<p>Given the way you set it up, you would get that 26 is a prime. Since it is only divisible by 13, and 13 is not yet in the table, it will fall through the while. You need to find all primes less than the number that you ask for to populate your table, or set the program find the first n primes.</p>

<p>I notice that the else on the if is in the wrong indentation. </p>

<p>You should use the modulo operator on the if and not division as you my be getting integer division and not floating point. </p>

<p>I put the fix in the code below.</p>

<p>Note that the problem is that you are using extend when you should be using append.</p>

<p><a href="http://stackoverflow.com/questions/252703/append-vs-extend">append vs. extend</a></p>

<p><a href="https://docs.python.org/2/library/array.html?#array.array.append" rel="nofollow"><code>append</code></a>: Appends object at end.</p>

<pre><code>x = [1, 2, 3]
x.append([4, 5])
print (x)
</code></pre>

<p>gives you: <code>[1, 2, 3, [4, 5]]</code></p>

<hr>

<p><a href="https://docs.python.org/2/library/array.html?#array.array.extend" rel="nofollow"><code>extend</code></a>: Extends list by appending elements from the iterable.</p>

<pre><code>x = [1, 2, 3]
x.extend([4, 5])
print (x)
</code></pre>

<p>gives you: <code>[1, 2, 3, 4, 5]</code></p>

<p>Here is the code set up using modulo for the test rather than division. This shows the correct situation. Note that when doing division and getting a floating point result isinstance() may not give the correct answer. There are a number of sources that recommend not using that as a test.</p>

<p>If you are using Python 3.0, then you would be getting a floating point response rather than an int answer. See <a href="http://stackoverflow.com/questions/1282945/python-integer-division-yields-float">Python integer division yields float</a></p>

<pre><code>prime = [2,3,5,7]
get = int(input("what number do you want to check ? "))    
a = 0
while a &lt; len(prime):
        print("Test " + str(a+1))
        if (get % prime[a]) == 0:
            # Number was evenly divisible by a prime number
            print(str(get) + " is not a prime number" )
            break
        else:
            a += 1                
else:
    # Number was not evenly divisible by primes already in table
    prime.append(get)
    print(str(get) + " is a prime number, prime number added to list"
</code></pre>

python primes