在python中打印列表

use*_*388 1 python printing list

我在尝试在python中打印列表时遇到问题.我想用下面的代码打印列表中的所有项目,特别是最后两行.

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
Run Code Online (Sandbox Code Playgroud)

我运行时遇到语法错误.它突出显示了最后一行中的"项目".据我所知,它在语法上是正确的,所以我很困惑!甚至这个网站显示的语法与我使用的语法相同http://effbot.org/zone/python-list.htm#looping

我出错的任何想法?

Sve*_*ach 14

您正在使用Python 2.x语法和3.x解释器. print是Python 3.x中的一个函数,所以你应该使用

print(item)
Run Code Online (Sandbox Code Playgroud)

代替.

  • 非常感谢你.我是python的新手,所以我真的需要习惯版本之间变化太大的东西! (3认同)