codecademy和IDLE的Python问题

时间:2014-04-24 12:46:11

标签: python

我是python的新手,我在codecademy上完成了这个挑战。

def spam():
    eggs = 12
    return eggs

print spam()

这适用于codecademy,但如果我把它放在IDLE中并运行它,我会收到一条错误,说错误的语法。所以我对正在发生的事情感到有些不满。任何人都可以提供帮助。

http://www.codecademy.com/courses/introduction-to-python-6WeG3/1/1?curriculum_id=4f89dab3d788890003000096

2 个答案:

答案 0 :(得分:2)

IDLE最有可能使用Python 3运行它,其中print()是一个函数。以下代码应该修复它。

def spam():
    eggs = 12
    return eggs

print(spam())

答案 1 :(得分:1)

你必须为Python 3.x运行IDLE,其中print不再是一个语句而是一个函数。因此,您需要将其称为:

def spam():
    eggs = 12
    return eggs

print(spam())

您的代码在CodeAcademy中工作,因为他们使用的是Python 2.x。