1

I am having issues trying to print a list in python. I want to print out all of the items in a list with the following code, specifically the last two lines.

def primefind(n):
  mylist = []
  x = 3
  while (x < n/2):
    if ((n % x) == 0):
      mylist.append(x)
      x = x + 2
  for item in mylist:
    print item

I am getting a syntax error when I run this. It highlights "item" in the last line. As far as I can tell it's syntactically correct so I am confused! Even this site shows the same syntax as I've used http://effbot.org/zone/python-list.htm#looping

Any ideas where I've gone wrong?

4

1 回答 1

15

You are using Python 2.x syntax with a 3.x interpreter. print is a function in Python 3.x, so you should use

print(item)

instead.

于 2012-04-09T17:41:26.303 回答