-1

I am very new to python and I have done this challenge on codecademy.

def spam():
    eggs = 12
    return eggs

print spam()

This works on codecademy but if I put it in IDLE and run it I get an error saying invalid syntax. So I am a bit contused about what is happening. Can any one help.

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

09stephenb
  • 9,358
  • 15
  • 53
  • 91
  • Check for the version of python you are working on. It should an issue in python3.0 – Workonphp Apr 24 '14 at 12:50
  • Coursera has a couple courses on Python 3.x if you're interested - Codecademy is still teaching 2.x (which is still popular amongst a lot of Python programmers and worth learning the differences of - but not the most recent version). – NDevox Apr 24 '14 at 12:52
  • (You could also install Python 2.7 if you're learning Python 2; a lot of the good learning material does still teach this version, and it's still very widely used. In a perfect world everyone would move to Python 3.4 immediately, but... it's not a perfect world.) – Wooble Apr 24 '14 at 13:11

2 Answers2

2

IDLE is most likely running it using Python 3, where print() is a function. The below code should fix it.

def spam():
    eggs = 12
    return eggs

print(spam())
sdamashek
  • 636
  • 1
  • 4
  • 13
1

You must be running IDLE for Python 3.x, in which print is no longer a statement but a function. Thus, you need to call it as such:

def spam():
    eggs = 12
    return eggs

print(spam())

Your code worked in CodeAcademy because they are using Python 2.x.