#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Canada
    Posts
    51
    Rep Power
    2
    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.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    Is 13 prime? How about 7919?
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo