Codecademy code not working in Python Shell


#1



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

Hello there, I was trying to run this code in a python shell on my computer but it says:


D:\Programming\Python>cc.py
  File "D:\Programming\Python\cc.py", line 5
    print spam()
             ^
SyntaxError: invalid syntax

Here is the code I tried:

def spam():
    eggs = 12
    return eggs
        
print spam()

Thanks in advance!


#2

codecademy uses python2, are you using python2 or python3? there are some fundamental differences, for example, in python3, with print, you need brackets:

print(spam())

people weren't happy when python3 was released, since python2 programs cant run in python3 (no backward compatibility)


#3

Thanks, I was using python 3! Ugghh that's really annoying though.
Should I not bother with python 3 then? Are there any advantages of it, because other wise that's going to confuse my learning on codecademy...


#4

Now, that is a tricky question. Here is the problem: there are still plenty of programs/apps written in python2, but we are moving away from python2, so if you are building new apps, you are better of with python3

The changes are not that big, i would go for python3. Either way, you can do the codecademy course, many things are still the same


#5

Ok, thank you I will continue with Python 3!


#6

I suggest installing both, they will happily both exist on the same system.

New programs should be written in Python3. The differences are not huge, but as languages age it becomes apparent that certain things would be better designed in a different way. There's then a choice between having some bad/broken features or breaking with the old. They are both bad choices.

Again, the differences are not huge. Just learn the language in whichever, dealing with the differences is just something that we need to take in stride as programmers, constantly learning, solving problems and finding the information we need.