-3

I was doing a program that showed several prime numbers,is necessary to use functions. For example 1 at 10 = 2,3,5,7

but appear an error in the last line in the - print i- only ... I can't understand what is that

def isPrime(num):
    if num < 2:
        return False

    i = 2
    for i in range(2,int(math.sqrt(num)+1)):
        if (num % i == 0):
            return False

    return True

def main():
    print ("this program do prime numbers")
    start = int(raw_input("start number "))
    finish = int(raw_input('finish number: '))

    for i in range(start,finish):
        if isPrime(i):
            print i
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Susan
  • 25
  • 6
  • 1
    I find it helps to google error messages. `python print syntax error` would have solved the problem. (Note that there are other issues here-- you'll get a NameError from `raw_input`, for example, but googling will solve that too.) – DSM Sep 13 '14 at 23:16
  • @nneonneo - so, thinked is this too, but I tried to change, but not works – Susan Sep 13 '14 at 23:28
  • So what error do you get? – Tom Dalton Sep 14 '14 at 00:42
  • @Tom Dalton - invalid syntax error,it indicates the i, but tried to change for (i), but the same error appear ... I can't understand what's wrong – Susan Sep 14 '14 at 01:23
  • @Martijn Pieters - I think I didn't understand well ... sorry – Susan Sep 14 '14 at 01:26
  • @DSM - I researched a lot but I haven't found anything like my error T.T – Susan Sep 14 '14 at 01:28

1 Answers1

1

Your problem is that you are not surrounding the i in print i with parentheses. In Python 3, print has been changed from a statement to a function. Here is your edited code:

def isPrime(num):
    if num < 2:
        return False

    i = 2
    for i in range(2,int(math.sqrt(num)+1)):
        if (num % i == 0):
            return False

    return True

def main():
    print ("this program do prime numbers")
    start = int(raw_input("start number "))
    finish = int(raw_input('finish number: '))

    for i in range(start,finish):
        if isPrime(i):
            print(i) #Previously "print i"

Previously running:

bash-3.2$ python3.4 prime.py
  File "test.py", line 19
    print i
          ^
SyntaxError: invalid syntax
bash-3.2$

After editing:

bash-3.2$ python3.4 prime.py
bash-3.2$
ZenOfPython
  • 891
  • 6
  • 15