0

I am trying to learn some code, and found python. I'm only a beginner but intrested in a programming language.

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.

here is my code.

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

a = 0
while a < 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"
sabbahillel
  • 4,357
  • 1
  • 19
  • 36
  • In what way is it not working? – Robert Columbia Aug 29 '16 at 16:45
  • It cant really add number to the list, i get this traceback with pycharm: what number do you want to check ? 180 Traceback (most recent call last): File "C:/....../Primechecker.py", line 16, in prime.extend(get) TypeError: 'int' object is not iterable Test 1 Test 2 Test 3 Test 4 180 is a prime number, prime number added to list Process finished with exit code 1 – Yannick Stulens Aug 29 '16 at 16:52
  • Could you post the traceback? – Robert Columbia Aug 29 '16 at 16:53
  • Note that the if for the else is incorrectly indented and you may be using integer division rather than floating point division. I suggest modulo in the code as I show in my answer. – sabbahillel Aug 29 '16 at 17:19
  • 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 below the number that you ask for first or find the first n primes. – sabbahillel Aug 29 '16 at 17:39
  • Hi @Yannick, welcome. "Prime code not working" is not a good question title, please change it to a more informative title. – Ronen Ness Aug 29 '16 at 18:19

2 Answers2

2
prime.extend(get)

extend is used to add a list to a list. If you only want to add a single value, use append.

prime.append(get)
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • Thank you, it now runs without errors, but it does not work :s if i type in 8, it type test 1 through 4 and writes that 8 is a prime number, which it obviously is not, as it should get eliminated at the first test right ? can you help further ? – Yannick Stulens Aug 29 '16 at 17:06
  • @YannickStulens I mention that you should use modulo in the if in my answer. I am typing it in now to correct a typo. – sabbahillel Aug 29 '16 at 17:12
  • Oh, that's because your test isn't successfully testing if `get` is divisible by `prime[a]`. For example, 8 / 2 is 4.0, which is a float, not an int. (note that this behavior varies between Python versions; in 2.7, you would have gotten an int, but that's no longer the case for 3.0 and up) Try using modulus instead: `if prime[a] % get == 0:` – Kevin Aug 29 '16 at 17:18
  • Thanky you so much :) – Yannick Stulens Aug 29 '16 at 17:37
1

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.

I notice that the else on the if is in the wrong indentation.

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

I put the fix in the code below.

Note that the problem is that you are using extend when you should be using append.

append vs. extend

append: Appends object at end.

x = [1, 2, 3]
x.append([4, 5])
print (x)

gives you: [1, 2, 3, [4, 5]]


extend: Extends list by appending elements from the iterable.

x = [1, 2, 3]
x.extend([4, 5])
print (x)

gives you: [1, 2, 3, 4, 5]

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.

If you are using Python 3.0, then you would be getting a floating point response rather than an int answer. See Python integer division yields float

prime = [2,3,5,7]
get = int(input("what number do you want to check ? "))    
a = 0
while a < 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"
Community
  • 1
  • 1
sabbahillel
  • 4,357
  • 1
  • 19
  • 36