0

I am working on the following code:

def numberToName(number):
    if (number==3):
        return "Three"
    elif (number==2):
        return "Two"
    elif (number==1):
        return "One"
    else:
        return "Invalid"

print numberToName(2)
print numberToName(3)
print numberToName(1)
print numberToName(1)

This code runs 100% fine in the following online Python environment - http://www.codeskulptor.org/#user11_Hh0KVUpNVP_0.py

But when I use IDLE it shows a syntax error Invalid Syntax in line print numberToName(2)

My Python version is 3.3.1

I have noticed some issues as well. For an example, in the given URL, I can run print "hello" and get the output, but the same generated error in IDLE unless I type print ("Hello").

What is the issue here? I am new to Python.

(Please note the main question is about the given code snippet).

halfer
  • 19,824
  • 17
  • 99
  • 186
PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • If you plan to learn Python 3.3 (which is a good idea), you're going to want to find an online Python 3.3 interpreter. There are a bunch of them out there if you search. There's even an online [interactive visualizer](http://pythontutor.com/visualize.html#) that handles 3.3. – abarnert May 02 '13 at 20:17
  • 1
    Also, if you don't know which Python version you're using, you can always do this: `import sys; print(sys.version)`. This line of code works with both 3.x and 2.x (and even 1.x, in case you accidentally fall through a Primeval-style temporal anomaly). – abarnert May 02 '13 at 20:21
  • @msw: Wow, that's all kinds of weird. I love their handling of complex numbers—`j` is a legal suffix for numbers, but means nothing. So, e.g., `1+2j` is `3`. Imagine how easy (and useless) trig class would be… And simulating bitwise operators on floats is especially clever; you can prove that `((1 << 64) + 1) & 1) == 0`. – abarnert May 02 '13 at 21:09
  • @msw: No, it is a python environment. It is built by Rice University, specially for their online course in here https://www.coursera.org/course/interactivepython. I am also a student there – PeakGen May 03 '13 at 05:21
  • It's a Python-like environment. If you come here and ask why Python is telling you `((1 << 64) + 1) & 1) == 0` and `not dir(dict)` are True, people will waste time trying to figure out that you're running a simulation of Python written in javascript. Clever? Sure. Python? Nope. – msw May 03 '13 at 08:34

4 Answers4

4

The online environment is Python 2. Your IDLE install is Python 3.

There are many differences, in this case the issue is that print is now a function.

See also: http://python3porting.com/

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
3

That is because the IDE is using Python 3.X, and not Python 2.X like in the online environment.

In Python 2.X you can use print("test") or print "Text".

In Python 3.x you need to use print("test").

This is because in Python 2.X print is a keyword, and not a function, while in Python 3 it is a function.

If you change the print functions to this, it will run on both the web application and your IDE.

print(numberToName(2))
print(numberToName(3))
print(numberToName(1))
print(numberToName(1))
eandersson
  • 25,781
  • 8
  • 89
  • 110
3

The site you link to uses python 2.6 as they state.

print is no longer a statement in python 3, but rather a function, which means it must be called with parenthesis: print(...).

azgult
  • 542
  • 3
  • 10
1

The difference between print 'word' and print('word') was one of the big changes from 2.x to 3.x. To fix it, just treat print as a function and it will work.

erdekhayser
  • 6,537
  • 2
  • 37
  • 69