August 9th, 2013, 11:18 AM

Help Needed: Python Code Error
Hello All,
I am new to programming and would appreciate advice. My goal is to generate the first 1000 prime numbers and to print them out.
To do this, I created this code:
>>> primes = [] # empty list that I plan to fill
>>> i = 0 # counter to count number of elements in list above
>>> while i <= 1000: #while number of elements in list is 1000 or less
i = i + 1
for x in range(10000): #assumed that the first 1000 prime numbers are within this range
if x % 2 == 0 or x % 3 == 0 or x % 5 == 0 or x % 7 == 0:
pass
else:
primes.append[x] #line of error. attempted to add all values of x that are prime, or not divisible by the factors above, to the list.
>>>primes
When I tried to run the code I received an error message that stated:
"Traceback (most recent call last):
File "<pyshell#6>", line 5, in <module>
primes.append[x]
TypeError: 'builtin_function_or_method' object is not subscriptable"
What am I doing wrong? And is there a more efficient way to do this? Your advice in layman, beginner terms would be much appreciated.
August 9th, 2013, 12:08 PM

Originally Posted by #programmer_117
Hello All,
I am new to programming and would appreciate advice. My goal is to generate the first 1000 prime numbers and to print them out.
To do this, I created this code:
>>> primes = [] # empty list that I plan to fill
>>> i = 0 # counter to count number of elements in list above
>>> while i <= 1000: #while number of elements in list is 1000 or less
i = i + 1
for x in range(10000): #assumed that the first 1000 prime numbers are within this range
if x % 2 == 0 or x % 3 == 0 or x % 5 == 0 or x % 7 == 0:
pass
else:
primes.append[x] #line of error. attempted to add all values of x that are prime, or not divisible by the factors above, to the list.
>>>primes
When I tried to run the code I received an error message that stated:
"Traceback (most recent call last):
File "<pyshell#6>", line 5, in <module>
primes.append[x]
TypeError: 'builtin_function_or_method' object is not subscriptable"
What am I doing wrong? And is there a more efficient way to do this? Your advice in layman, beginner terms would be much appreciated.
Is this project euler, I think I remember doing this problem. anyways if you take a close look at the line that you know is the error, you'll probably notice the mistake. I'll give you a hint, the append method is called and not indexed.
Good luck!
Last edited by Good News; August 9th, 2013 at 02:26 PM.
August 9th, 2013, 02:03 PM

Is 13 prime? How about 7919?
[code]
Code tags[/code] are essential for python code and Makefiles!